Skip to main content
GameDev.net gamedev.net
🔒 Locked

I should know this

Started by CyberSlag5k Jun 3, 2006 at 9:58 PM 4 replies 800+ views
Original Post
CyberSlag5k
CyberSlag5k
But alas, I don't. I'd like to write my C++ constructors such that the only difference between the default constructor and one with parameters is only the changes made by the parameters. Thereby easing maintainance by requiring that I only modify the default constructor, and the changes will trickle down. For example:

class myClass
{
public:
myClass();
myClass(const int& X, const int& Y);
private:
int x, y, z;
}

myClass::myClass()
{
x = 1;
y = 2;
z = 3;
}

myClass::myClass(const int& X, const int& Y)
{
x = X;
y = Y;
}

With this class the way it is now, if I were to do this: myClass thing1(); myClass thing2(5, 6); cout << thing1 << endl << thing2 << endl; the output would be: 123 560 where that 0 could be just about anything, and it probably would not be 3. Now I know I could just use default values to set x, y, and z if they were not supplied, but that only works in simple cases such as this. If I wanted a function to be called in the default constructor, for example, and wanted that same function called in the other constructor, I'd have to type it twice. Not that this is the worst thing in the world, but it would be very nice if I did not have to. Is there a way to make something like myClass(const int& X, const int& Y) : myClass() work?
Without order nothing can exist - without chaos nothing can evolve.
CyberSlag5k
CyberSlag5k
/cry

Thanks, Sneftel.
Without order nothing can exist - without chaos nothing can evolve.
MaulingMonkey
MaulingMonkey
Quote:
Original post by CyberSlag5k
Is there a way to make something like

myClass(const int& X, const int& Y) : myClass()

work?


myClass( const int & X , const int & Y ) { common_init(); }myClass( const int & X , const int & Y ) : myClassBase() {}


Overkill for this situation, but useful in more complex ones.
CyberSlag5k
CyberSlag5k
Having an initialization function is an excellent way to emulate the behavior I want. It makes construction a bit heftier, but if I've done my job I'll have minimized the number of objects being created anyway. What's silly is that I do that for my destructors to avoid redundant clean-up code. It should have occurred to me to do the same. But I am a silly silly man. Thanks, MaulingMonkey!

What's up with the second line, though? Is that meant to be the class's declaration, calling its base's constructor as well?
Without order nothing can exist - without chaos nothing can evolve.
MaulingMonkey
MaulingMonkey
Quote:
Original post by CyberSlag5k
Having an initialization function is an excellent way to emulate the behavior I want. It makes construction a bit heftier, but if I've done my job I'll have minimized the number of objects being created anyway. What's silly is that I do that for my destructors to avoid redundant clean-up code. It should have occurred to me to do the same. But I am a silly silly man. Thanks, MaulingMonkey!

What's up with the second line, though? Is that meant to be the class's declaration, calling its base's constructor as well?


Yes. In this case, you actually don't need it since the default will be called anyways :-). More useful for situations where certain members /require/ non-default construction, but the same one.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.