Thread: Namespaces or package equivalent in C++

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    20

    Namespaces or package equivalent in C++

    Is there an equivalent of namespace or package in C++?

    For instance,

    In C#:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace MyProgram
    {
        public class Program
        {
            static void Main(string[] args)
            {
             ...
            }
        }
    }
    In Java:
    Code:
    package MyProgram;
    
    public class Main
    {
        public static void main(String[] args)
        {
         ...
        }
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Yes. It is called namespace, although I'm not sure if it is equivalent to a Java package.

  3. #3
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    in C++

    Code:
    namespace someting
    {
    
    class someClass {};
    ...
    
    }
    to access methods and classes from the namespace
    Code:
    using namespace std;
    or
    Code:
    using std::someClass
    the first using declaration says to bring all classes in the namespace std into scope, so you don't have to use the scope operator for each class you want.

    the second one says that you just want to use a certain class within the namespace.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Or just put:
    Code:
    std::someClass
    everywhere you want to use the someClass name. That is the generally preferred method for the std namespace. Whether you use a using directive or using declaration like in indigo0086's examples depends how cumbersome the namespace(s) you are using are and how likely there is to be a name conflict in the scope you use it.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    20
    hey, thanks for the quick replies, everyone

    so basically, for each class that is related to one whole namespace, i just put them between the namespace braces?

    e.g.
    Code:
    #include <iostream>
    
    using namespace std;
    
    namespace something {
       class something {
    
       }
    
    }
    ?

    and what about my main class? do i put it in the same namespace as well?

    (this is my first time coding in c++, so i'm a total noob)

  6. #6
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    I usually have the using std::whatever to know what I'm using in my classes, unless it's something I'm not going to use often.

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    20
    Err sorry, what do you mean by using the "using std::something" to know what you're using in your classes?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    We actually have an FAQ on C++ namespaces around here.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by markcls
    Err sorry, what do you mean by using the "using std::something" to know what you're using in your classes?
    He means he'll do something like this:

    Code:
    using std::cout;
    So later on he can do:

    Code:
    cout << "This is part of a line.";
    Otherwise he would have to do:

    Code:
    std::cout << "This is part of a line.";
    Or else he would have to use the entire std namespace.

  10. #10
    Registered User
    Join Date
    Mar 2007
    Posts
    20
    Ohhhh ok! Thanks

    And omg, you guys are efficient. Wow.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    There's no main class in C++. Learn the language from the ground up.

    Another difference to C#: there's no access control in namespaces. Everything is, effectively, always public.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get RSSI value, send to sensor, sensor receive package, repackage it?
    By techissue2008 in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-04-2009, 10:13 AM
  2. Cannot install lrzsz package, please help
    By ssharish2005 in forum Tech Board
    Replies: 2
    Last Post: 06-20-2007, 05:36 AM
  3. Pointer equivalent to array notation
    By bekkilyn in forum C Programming
    Replies: 4
    Last Post: 12-06-2006, 08:22 PM
  4. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  5. Possible Loss of data
    By silicon in forum C Programming
    Replies: 3
    Last Post: 03-24-2004, 12:25 PM