Jump to content
xisto Community
Sign in to follow this  
Vyoma

C++ Namespace when to use them?

Recommended Posts

Background: I got back into C++ programming after some leave from it. :rolleyes:More on that experience in my other thread. I started this particular project (initially, would be hobby - may GPL it if results are good).

What is the directive on using namespaces in C++?

Coming from Java background, the packages which translates to namespaces in C++ (from what I understand), is required to avoid the crazyness it brings if we don't use. For example, the generated (proxy) source files (either from WSDL or XSDs using WSDL2Java or JAXB respectively) to communicate with other teams systems, can cause conflicts if we dont target each of these to their own packages. And hence, it makes sense to use packages for our own Java classes. It avoids conflicts and this further promotes better refactoring and reuse of developed and tested components.

Back to this C++ project. It would end up as a application/executable. I do not think it would be a library ( STL, GTK+, QT, gtkmm, mysql etc). Even in such cases, is there a point of using namespaces? I am not sure I can see any advantage of this. Perhaps my understanding on C++ Namespaces is wrong?

Any thoughts? Or even links that describes "when to use C++ namespaces" would be much appreciated.

Share this post


Link to post
Share on other sites

Well, namespaces in C++ are mainly used for organization. When you produce your own libraries, namespaces come in handy for the end-user, since they make sure he/she will not run into compile-time errors due to conflicting declarations.

 

But since you're planning to produce an executable, using namespaces is generally a matter of personal preference -- emphasis on "generally." Sometimes, however, different compilers will deal with this differently. I openly admit that I used to have some hard times with namespaces, particularly with Microsoft Visual C++ (it's very permissive when it comes to good coding practices.)

 

Anyway, if you have the time and are interested in a comprehensive understanding of the theory and practical use of namespaces in C++, I highly recommend taking a look at Bruce Eckel's masterpiece, Thinking in C++. It's a free e-book that is available for download in different formats.

 

Volume I walks you through a rather encyclopedic view of the language, while still giving you enough examples to move the discussion from the purely-theoretical to the usefully-applicable. You can download Volume I using the link below...

 

http://forums.xisto.com/no_longer_exists/

 

Volume II: Practical Programming takes you down the road of actual programming projects, pointing you towards good, practical methods for completing projects optimally and introducing a wide range of helpful tips and techniques. It's generally more advanced than Volume I and it largely depends on it -- more precisely, it assumes a somewhat high familiarity with the C++ language. You can download Volume II using the link below...

 

http://forums.xisto.com/no_longer_exists/

 

Sorry if my comment isn't specific enough to answer your question, but I trust someone else will have a much better response :rolleyes:

 

Good luck.

Share this post


Link to post
Share on other sites

Dont think there is an issue with your reply not being specific. Those links that you gave seem to give me enough to read through and understand the concepts I had missed the past 4 years. :rolleyes: Thanks!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.