namespace vs classes in C++
In C++,
1 - What's the difference between a namespace and a class? A namespace replaces the class or vice-versa? 2 - If i've a class defined inside a namespace, in a header file, how should i write the methods to implement in the cpp file? For example: test.h Code:
namespace Test{ 3 - Namespace and class can have the same name? If not, is there a rule to define namespace and classes names (for example, namespaces should always be written in capitalize letters, or the classes should always start with a capitalize letter)? Thanks, Pedro |
Quote:
Quote:
Code:
namespace TEST Code:
void TEST::Test::inc(int i) Quote:
|
I look at a class as a description of what a variable will look like, you can create many variables which are based on the class description, each will have their own unique state. The class will describe each of the different data values it will have, along with the different functions that can operate upon this data.
In OO speak objects are created based on the class, The state of an object is defined by the value of the member variables that are held within the object, member functions can be used to adjust the value of these member variables, hence changing the state of each object. I think of a namespace as a convenient mechanism of holding related functions and occasionally variables together. It can help to ensuring that there will not me a class of function names. Now let's assume that you have written a library that sends data along a raw socket, it will probably have a function called send. Now I might see that library and think wonderful I want to use that in my latest network management program, everything is going fine until I hook into my program another library that sends email messages, that also have a function called send. Without namespaces I'd be stuck, thankfully you would of used names paces so I can clearly instruct the compiler which send function to use, RAW_SOCKET::send() or EMAIL::send(). Neither of the aforementioned libraries use classes, but they use namespaces to ensure that the labels used in their (to borrow a Java term) package can be uniquely identified. |
Thanks for your reply
|
All times are GMT -5. The time now is 04:28 PM. |