🔒 Locked
Using namespaces
Advertisement
Original Post
I started making my project and got rather far... the problem with it was that I was using classes for things that should not have been classes. I ended up having classes for DirectInput, OpenGL drawing routines, making a window, starting a new thread, etc... each of these classes with one global instance, it might as well just be a bunch of global functions...
Thing is I''m just not comfortable using any globals other than main(). Is it a good design decision to be using namespaces (rather than useless one-global-instance classes) to seperate my functions so I can divy up the global namespace? I''m fairly new to using C++, but have been using C for a about a year. I want my code to be very well organized because I know there will be lots of source code.
I am no OOP guru, but I think you have a good idea.
If the classes you mentioned don''t have any data members, I think they should be namespaces indeed.
The previous lead programmer on my project used classes with static functions instead of namespaces. I''m stuck with that.
Go ahead and be lucky.
On the other hand, if your classes do have data members, maybe you could use a singleton. (Search gamedev for the singleton idiom)
If the classes you mentioned don''t have any data members, I think they should be namespaces indeed.
The previous lead programmer on my project used classes with static functions instead of namespaces. I''m stuck with that.
Go ahead and be lucky.
On the other hand, if your classes do have data members, maybe you could use a singleton. (Search gamedev for the singleton idiom)
I considered using singletons too, and I could make each member / function of a class static, but then I''m still stuck with having to make an instance of a class that I would rather not have to make.
I figured out a nice trick using an "unnamed namespace" to mimic private member functions of a class too.
The only problem with using the namespaces is that for some of my old classes the destructor handled any resource / memory unloading, and with namespaces I would need a seperate shutdown function.
I just don''t like using a class for something that only needs to be used once during the entire program...
I figured out a nice trick using an "unnamed namespace" to mimic private member functions of a class too.
The only problem with using the namespaces is that for some of my old classes the destructor handled any resource / memory unloading, and with namespaces I would need a seperate shutdown function.
I just don''t like using a class for something that only needs to be used once during the entire program...
The biggest issue I have with namespace is readability and maintainability. If you get in the habit of namespaces you will soon find yourself reading code and wondering... "Where is that function?" or "Who owns that function?" Especially if you didn''t write the code!
I know this from experience. I was trying to use/learn a physics API that used some namespacing in it''s source. It was confusing and a hastle ~ we finally canned it.
As a style decision, my team has chosen not to use namespace at all.
I would suggest having it as a static member of a static class and inlining the access call (if necessary). Another idea would be to pull together global elements into just one class. I have done this before and it has worked well.
I know this from experience. I was trying to use/learn a physics API that used some namespacing in it''s source. It was confusing and a hastle ~ we finally canned it.
As a style decision, my team has chosen not to use namespace at all.
I would suggest having it as a static member of a static class and inlining the access call (if necessary). Another idea would be to pull together global elements into just one class. I have done this before and it has worked well.
If questions like this come up, you should ask yourself whether your design is any good. From what you have told, it doesnt seem very well thought out. You seem to be stuck in a procedural mindset(one of the reasons I always say people shouldn''t learn C before learning C++).
Fantastic doctrines (like Christianity or Islam or Marxism or Microsoft-bashing) require unanimity of belief. One dissenter casts doubt on the creed of millions. Thus the fear and hate; thus the torture chamber, the iron stake, the gallows, the labor camp, the psychiatric ward - Edward Abbey
Fantastic doctrines (like Christianity or Islam or Marxism or Microsoft-bashing) require unanimity of belief. One dissenter casts doubt on the creed of millions. Thus the fear and hate; thus the torture chamber, the iron stake, the gallows, the labor camp, the psychiatric ward - Edward Abbey
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement