Sign in to follow this  

Great Life Saver

This topic is 3848 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

This topic is 3848 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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