Exceptions & namespaces

Started by
14 comments, last by MaulingMonkey 17 years, 6 months ago
Hi all! Well I´m prototyping a game for a project using C++ that is going to be quite long for my standards (at least 4 months I presume), so I´ve been pondering on some questions regarding code organization and error handling: 1. Is using exceptions the best way to make my code handle errors properly? I´m used to writing conditionals galore to catch them, but maybe exceptions provide better performance? 2. If so, are there any pre-built exceptions on Visual C++ 6.0? Or do I have to write an exception class? I know when using .NET Framework you can do both, but is that something only for .NET or a standard C++ feature? 3. I´m using namespaces to separate groups of functions with different general objectives on my code. Is this a good thing in terms of organization, or should I divide them in classes as static functions (I suppose instantiating a class just to use a function, the class having no attributes at all, is a dumb thing to do)? What´s the underlying difference between them? And also, where could I find a good exception handling tutorial using C++? I´ve read some but they don´t seem to be very good. Thanks in advance, Carol
Advertisement
I'd recommend reading this interview with Bjarne Stroustrup for some discussion on the use and misuse of classes.

It's easy to go overboard with classes. Not everything needs to be a class. Some things don't make sense as classes. Sometimes you don't even need to hide every single data member of a class. If a function doesn't need access to the internal representation of a class it doesn't need to be a member function and classes aren't meant to group functions together as libraries. He touches on some of those issues in this interview and on his site.

It's a good idea to encapsulate your data structues and functions in namespaces and provide interfaces to that namespace for maintainability issues.
You mentioned Visual Studios 6.0. Stop right there, drop it, delete it and break the CDs if you still have them. Once broken, burn them. VC6 is horrifically broken and doesn't even support the C++ standard as it is today or when it was first "standardized". Get the FREE express edition. Make your life a LOT easier.
Huh... you say VC6 doesn´t follow standards, but before using it I was stuck with Dev-C++ and I must say VC6 is a lot better, and it seems to be the same C++ as Dev´s. I got it free from college so why not use it? Besides, the Express edition uses C++ with .NET only, right? Although I guess .NET really makes life easier, I´d rather program my GUI using GDI+ just so I can learn how (I´m just making a supposition here) .NET forms and stuff like that work behind the scene.
I´d appreciate opinions on that, though =)

Carol
no, the express edition is not .NET only. You just have to download the platform SDK seperately and install it too. do a search for either "visual C++ express edition native app" or "Visual C++ express edition platform sdk" because there are many beginners here that have asked that question. Also, Visual Studio 2005 in general trips people up because it changed the default string / unicode options from VC 6 and VS 2003 - so you may need to read a few topics on that too, but the gist is you will probably want to change your project's string options too.

VC 7.1 (2003) was perhaps the very best native C++ compiler because it had very good standards support without changing the options people we're used to. VS 8 (2005) can be just as good, you just have to be aware of some changes.
VC6 isn't really as bad as everybody makes it out to be. It is however about 8 years old making it positively ancient in computer years and support for modern coding styles is rather lacking. There is really no reason whatsoever to use it, especially when the most recent VC/C++ is available for free.

1. "best" is subjective. There have been several flamewars on this board about whether or not exceptions are worth using at all, and if so, to what degree. Having said that, I like them. Take that for what you will.

2. There really isn't any reason not to derive your app-specific exceptions from std::exception or one of it's standard subclasses IMHO.

3. If the choice is between free functions in namespaces and static member functions in otherwise empty classes it doesn't really matter IMHO. I would rate the latter rather higher on the wtf? scale though.
-Mike
Just jumping by to support the destruction of VC6 :-)
The VC6 compiler was written before the c++ standard was released in 98, and so it doesn't support MANY important c++ features. Dev c++ is working with the gcc compiler, which is much better than VC6's compiler, but the enviroment isn't that good. VC2003/2005 is the way to go, as Xai said above.

As for your other question: exceptions are great (though very heavy on the code size, if that matters to you for some reason). I know no other convenient way to handle errors, so using exceptions is definitly the way to go.
Yoni Levy.
Quote:Original post by Carolina
I got it free from college so why not use it?


Because it's like AIDS.

Remember kids, just say no to unprotected sex no longer officially supported products (Microsoft no longer supports DirectX for VS6), which have been superceeded by vastly superior solutions (such as the free Visual Studio 2005 Express) without doing your homework first (which you have not, if you've laboured under the false assumption that VS2005 is .NET only).

In seriousness, I don't consider VS6 a C++ compiler. It predates the standard, and as such deviates heavily from the C++ standard in terms of both templates and it's standard library.
Wow, so NOW I know why VC6 was so easy to get! Well, then, guess it´s bye bye to this ancient & evil thing, and I´ll download the Express version XD
You only got an 8 year old piece of software free from college? I got WinXP and Visual Studio 2003(I can get 2005 as well, I just had a class that wanted us to use 2003). I can get Visio as well as several other MS products for the cost of the CDs it takes to hold the installation disks. Not trying to gloat, but are you sure your college doesn't have access to the Microsoft Developer Network Academic Alliance(MSDN AA)? This program allows participating colleges to give out a lot of software for free to students in a computer discipline. About the only thing they Don't have is MS Office.

Just something to look into.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

This topic is closed to new replies.

Advertisement