Great Life Saver

Started by
6 comments, last by PhoenixAdmin 16 years, 10 months ago
Sometimes or most of the time when you create objects(class). If the Class constructor doesn't have parameters then your forced to have all the objects of that class be initialized at the same type there created which works to a certain point but then you might want some user interactivity or for some other reason. The only way you can fix this is to Overload your class constructor like this: Class randomclass{ public: //constructor's randomclass(){statments} randomclass(parameters){statments} } Now you can make randomclass a; or randomclass a=3; This works better with arrays.
Advertisement
While this may be very useful, you should reword this a bit to make it clearer.

Also, what I recommend you do is compile a giant list of tips and guidelines, make it the form of a tutorial then talk to someone who can edit it and make sure it is indeed helpful/true and then submit it as a tutorial to Gamedev.
My connection failed... So I had to live it like that but as you can see I finished it.
So many gotchas here.

Construction of objects isn't to be taken lightly.

First problem is self-assignment.
Assuming your constructor allocates data. Passing itself can quickly result in corrupted state. I won't go into details, there's plenty of them elsewhere.

Second is that what you're demonstrating mentions constructors, but doesn't mention overloading assignment operator. Once again, common topic.

Then there's implicit conversions. Simply putting in arbitrary constructors can quickly lead to weird issues, where classes are constructed using invalid paths. Convenience, and just about all other constructors should be marked explicit.

Then there's the choice of parameters. const, non-const, by value, by reference? Each has pros and cons. There's also a matter of inherited objects and potential issues when assigning them.

Another issue is temporaries, which can get created when using assignments or at least various allocation schemes. This isn't obvious when using primitive types, but can cause overhead with complex classes, or even more complex problems when allocating objects with comples state.

There's also default parameters. Consider this:
class Foo{public:  Foo( size_t default_size = 10 );}


Here, you get two constructors for the price of one, including the default one.

Then there's also rule-of-three. Quite important when dealing with inheritance, and something to always consider when defining object creation.

*Never* trade convenience without considering all the implications. This is double true for C++. By saving typing of 2 letters you've just created a maintainance nightmare.

Fully compliant, re-entrant, copy and assignment-safe implementation of a class is extremly long. And even then, some classes simply cannot be made perfectly safe.
Okay?
ROFL I hope this thread becomes as interesting as the forever loop thread.
class Foo{    Foo( size_t default_size )    {       for (;;)        {           crying:           Jesus::Baby->getInstance()->cry();           goto( crying ); //Code speed hole       }    }    bool IsNegative(unsigned int iCanNeverBeNegative);};


[Edited by - Leviathan3328 on June 4, 2007 5:54:18 PM]
There is agian nothing really wrong with this its just Function overloading.

This topic is closed to new replies.

Advertisement