Namespaces or package equivalent in C++

This is a discussion on Namespaces or package equivalent in C++ within the C++ Programming forums, part of the General Programming Boards category; Is there an equivalent of namespace or package in C++? For instance, In C#: Code: using System; using System.Collections.Generic; using ...

  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,340
    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,340
    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
    21,941
    We actually have an FAQ on C++ namespaces around here.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,211
    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,893
    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, 09: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, 07:22 PM
  4. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 10:31 PM
  5. Possible Loss of data
    By silicon in forum C Programming
    Replies: 3
    Last Post: 03-24-2004, 11:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21