Find the answer to your Linux question:
Results 1 to 9 of 9
Other than a comparison of semantics in the same sentence I can't find a definition for what it is or the context to how it works. Thanks in advance!...
  1. #1
    Linux Newbie theKbStockpiler's Avatar
    Join Date
    Sep 2010
    Location
    Upstate NY
    Posts
    195

    [SOLVED] What exactly is a Namespace in OOP?



    Other than a comparison of semantics in the same sentence I can't find a definition for what it is or the context to how it works.


    Thanks in advance!

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Imagine that we have a programming language that like many languages, has a "string" class. It does what you expect.

    Now suppose that I write a library that includes functions to deal with sewing. I might also want to write a "string" class for my sewing object model.

    Now we have two different string classes. We could name them different things (standardString and sewingString), but now we have very long names. Plus, now even if people don't want to use my sewing library, they need to use the long name for the standard string.

    Namespaces are basically a way to avoid collisions in, well, names. By having each library use its own namespace, applications can choose to use fully qualified names (e.g. std::string in C++), or if they're only using names that are unambiguous, they can use the short names (e.g. string).

    Make sense?
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Linux Newbie theKbStockpiler's Avatar
    Join Date
    Sep 2010
    Location
    Upstate NY
    Posts
    195

    Thanks for the Reply!

    I understand the vague concept but how does the different function call the correct library? How does it fork to the correct one or how is it's context changed?

  4. #4
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Let's look at a C++ program.

    Code:
    #include <string>
    #include <iostream>
    
    using std::string;
    
    int main(int argc, char **argv) {
        string mystring = "foo";
    
        std::cout << mystring << std::endl;
    
        return 0;
    }
    In this program, I am using the "std" namespace heavily. However, I use it in two ways.

    In C++, there is no "string" class. Instead, there is the std::string class, which is to say, a class called string declared in the std namespace. In my program, I decided that I don't want to have to type out its full name. So early in the program, I tell the C++ compiler "Whenever I talk about an unqualified string class, I am referring to std::string". I could also explicitly refer to sewing::string, but if the class name is unqualified, I am referring to std::string.

    On the other hand, I explicitly refer to the cout and endl objects that exist in the std namespace by including the namespace when I use them. If there were other cout and endl objects, I would need to explicitly qualify them too (e.g. others::cout).

    This only matters to the compiler, so that it knows which "string" class I mean. As far as the compiled program is concerned, I only ever referred to the fully qualified std::string.
    DISTRO=Arch
    Registered Linux User #388732

  5. #5
    Just Joined!
    Join Date
    Jan 2011
    Posts
    1
    Quote Originally Posted by Cabhan View Post
    Imagine that we have a programming language that like many languages, has a "string" class. It does what you expect.

    Now suppose that I write a library that includes functions to deal with sewing. I might also want to write a "string" class for my sewing object model.

    Now we have two different string classes. We could name them different things (standardString and sewingString), but now we have very long names. Plus, now even if people don't want to use my sewing library, they need to use the long name for the standard string.

    Namespaces are basically a way to avoid collisions in, well, names. By having each library use its own namespace, applications can choose to use fully qualified names (e.g. std::string in C++), or if they're only using names that are unambiguous, they can use the short names (e.g. string).

    Make sense?
    you are saying right ,i agree with you.

  6. #6
    Linux User Manko10's Avatar
    Join Date
    Sep 2010
    Posts
    250
    Imagine it as folders like those you have on your computer.
    As the others have already explained very well: they give you the opportunity to create several files with the same name and they also categorize them. Some namespace conventions are pretty odd because they only consist of domain names etc. (Java guys use this often) but if you have a good namespace system it also describes clearly what this class is about (e.g. system::io::cli::output instead of org::example::myFunnyWebpage::output).
    Refining Linux Advent calendar: “24 Outstanding ZSH Gems

  7. #7
    Linux Newbie theKbStockpiler's Avatar
    Join Date
    Sep 2010
    Location
    Upstate NY
    Posts
    195

    Too much too soon.

    I have studied C in the past and wanted to understand the Concepts of C++ for using Glade. I can't understand advanced concepts without understanding the basics which is what the definition of (Namespace) is. If namespace is used to explain namespace it does not help.

    Imagine it as folders like those you have on your computer.
    As the others have already explained very well: they give you the opportunity to create several files with the same name and they also categorize them. Some namespace conventions are pretty odd because they only consist of domain names etc. (Java guys use this often) but if you have a good namespace system it also describes clearly what this class is about (e.g. system::io::cli::output instead of org::example::myFunnyWebpage::output).



    Cabhan Let's look at a C++ program.

    Code:
    #include <string>
    #include <iostream>

    using std::string;

    int main(int argc, char **argv) {
    string mystring = "foo";

    std::cout << mystring << std::endl;

    return 0;
    }
    In this program, I am using the "std" namespace heavily. However, I use it in two ways.

    In C++, there is no "string" class. Instead, there is the std::string class, which is to say, a class called string declared in the std namespace. In my program, I decided that I don't want to have to type out its full name. So early in the program, I tell the C++ compiler "Whenever I talk about an unqualified string class, I am referring to std::string". I could also explicitly refer to sewing::string, but if the class name is unqualified, I am referring to std::string.

    On the other hand, I explicitly refer to the cout and endl objects that exist in the std namespace by including the namespace when I use them. If there were other cout and endl objects, I would need to explicitly qualify them too (e.g. others::cout).

    This only matters to the compiler, so that it knows which "string" class I mean. As far as the compiled program is concerned, I only ever referred to the fully qualified std::string.
    I'm trying to Learn C++ concepts so if you use them to explain something I can't understand it.I understand part of this explanation but does qualifying mean and how does it come into play?
    Last edited by theKbStockpiler; 01-20-2011 at 03:51 AM.

  8. #8
    Linux User Manko10's Avatar
    Join Date
    Sep 2010
    Posts
    250
    I think, my explanation is pretty clear. If you still don't understand what namespaces are good for, then you should read this: Namespace - Wikipedia, the free encyclopedia
    Refining Linux Advent calendar: “24 Outstanding ZSH Gems

  9. #9
    Linux Newbie theKbStockpiler's Avatar
    Join Date
    Sep 2010
    Location
    Upstate NY
    Posts
    195

    Thanks for the Replies!

    It's looking synonymous to arguments and parameters in C where the only info has to be analyzed guessed and narrowed down like a mystery. I will look into Java Packages to try to find out if namespace's are a verb, a noun or whatever. Incidentally a argument is a particular value of a parameter and are not the same thing and should not be used interchangeably.

    Serendipity to All?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...