Call constructor
-
Hello everyone, Is it legal and good code to call one constructor from another in the same class? I show the pseudo code below.
Test(bool param1, bool param2)
{
// set and manipulate param1// call constructor with a single bool input parameter Test (param2);
}
Test(bool param2) : active (param2)
{
// do something
}thanks in advance, George
-
Hello everyone, Is it legal and good code to call one constructor from another in the same class? I show the pseudo code below.
Test(bool param1, bool param2)
{
// set and manipulate param1// call constructor with a single bool input parameter Test (param2);
}
Test(bool param2) : active (param2)
{
// do something
}thanks in advance, George
-
No, you can't do that. It will just create a temporary object, but not calling the other constructor on the same object. You can write your initialization code in a separate function, and call the initialization function from each constructor.
Thanks fefe.wyx, Question answered. regards, George
-
Hello everyone, Is it legal and good code to call one constructor from another in the same class? I show the pseudo code below.
Test(bool param1, bool param2)
{
// set and manipulate param1// call constructor with a single bool input parameter Test (param2);
}
Test(bool param2) : active (param2)
{
// do something
}thanks in advance, George
George_George wrote:
Is it legal and good code to call one constructor from another in the same class?
Currently no, but the next version of the Standard includes delegating constructors[^] which will enable you to get such functionality, although with a slightly different syntax.
-
George_George wrote:
Is it legal and good code to call one constructor from another in the same class?
Currently no, but the next version of the Standard includes delegating constructors[^] which will enable you to get such functionality, although with a slightly different syntax.
Thanks Nemanja, I assume in current version of C++ and for common vendors/compilers, like MSVC 2008/2005, it is not supported, right? regards, George
-
Thanks Nemanja, I assume in current version of C++ and for common vendors/compilers, like MSVC 2008/2005, it is not supported, right? regards, George
George_George wrote:
I assume in current version of C++ and for common vendors/compilers, like MSVC 2008/2005, it is not supported, right?
Right. I bet it would show up in gcc first. Keep an eye on Status of Experimental C++0x Support in GCC 4.3[^]
-
George_George wrote:
I assume in current version of C++ and for common vendors/compilers, like MSVC 2008/2005, it is not supported, right?
Right. I bet it would show up in gcc first. Keep an eye on Status of Experimental C++0x Support in GCC 4.3[^]
Thanks Nemanja, Question answered. regards, George