class Test
{
public:
Test()
{
// here I want to call the other constructor
// Test(100); this won''t work, because it creates a new Test object, leaving me with two test objects.
}
Test(int field)
{
this->field = field;
}
private:
int field;
};
|
🔒 Locked
calling constructor from other constructor
Advertisement
Original Post
Hello everyone,
I have the following problem. How do I call a constructor from within another constructor of the same class. To make it even clearer, take a look at the source:
Of course, for such a simple example I could just do field = 100, instead of calling the other constructor, but where I need it in my program, it is more convenient to do it with the constructor.
Thanks
You don''t get to do this. C++ refuses to treat constructions like the functions they are.
The only way you''re going to get this working is to have the constructors call real functions that do their initialisation, so ''Test(int x)'' would call ''init(x)'', and then ''Test()'' would called ''init(100)''.
Just Plain Wrong
The only way you''re going to get this working is to have the constructors call real functions that do their initialisation, so ''Test(int x)'' would call ''init(x)'', and then ''Test()'' would called ''init(100)''.
Just Plain Wrong
Hey,
thanks for the answers. No Jon, in this example it would be the easiest solution, but in my program I have this situation.
Now, my code doesn''t have 9 ints of course, it has lots of different types (Strings, images, RECTs, ...). So I have to initialize them all separately. Then I also have five different constructors and I hate to see the five initializations the same time (with some minor changes).
So, I guess I''ll have to use the idea of the Init() method Mayrel proposed.
thanks for the answers. No Jon, in this example it would be the easiest solution, but in my program I have this situation.
|
Now, my code doesn''t have 9 ints of course, it has lots of different types (Strings, images, RECTs, ...). So I have to initialize them all separately. Then I also have five different constructors and I hate to see the five initializations the same time (with some minor changes).
So, I guess I''ll have to use the idea of the Init() method Mayrel proposed.
What about this :
class Test
{
Test( int a=0, int b=0, ...);
};
class Test
{
Test( int a=0, int b=0, ...);
};
quote:
Original post by Prosper/LOADED
class Test
{
Test( int a=0, int b=0, ...);
};
Default arguments is a good suggestion. The only thing is that the order is fixed, meaning you can''t specify override values for arguments 1 and 3, omitting 2. Other than that, though, this might be the best solution.
quote:
Original post by Anonymous Poster
not sure if this is smart but what about placement new? Something like new(this)();
That doesn''t look to good. I don''t even know what you are trying to tell us
Anyway, that thing with using default arguments is not going to help me either, just because of the problem that Oluseyi mentioned.
That means, I''ll still be using the Init() method.
I was going to suggest placement new as well (which, btw Mayrel, is how you treat constructors like the functions that they are
) It's not any more flexible than default arguements, so if that doesn't help, placement new probably won't either.
The hidden Init method is what is suggested in Effective C++.
Magmai Kai Holmlor
- Not For Rent
Edited by - Magmai Kai Holmlor on December 4, 2001 12:16:45 PM
The hidden Init method is what is suggested in Effective C++.
|
Magmai Kai Holmlor
- Not For Rent
Edited by - Magmai Kai Holmlor on December 4, 2001 12:16:45 PM
What about templates?
|
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement