Does C++ allow a constructor to call another constructor of the sameclass?

W

Warren Tang

Hello,

Does C++ allow a constructor to call another constructor of the same
class? (I am focusing on the language abilities. I know the C# language
allow this.)

The following sample can explain this question more clearer:

class MyClass
{
public:
MyClass()
{
//Do something.
}
MyClass(int a, int b)
{
//I need to call the parameter-less constructor. How?
}

Thank you for your attention.

Regards
Warren
 
I

Ian Collins

Warren said:
Hello,

Does C++ allow a constructor to call another constructor of the same
class?

No, if you have common initialisation code, it can go in a member function.
 
A

Alexander Dong Back Kim

Hello,

Does C++ allow a constructor to call another constructor of the same
class? (I am focusing on the language abilities. I know the C# language
allow this.)

The following sample can explain this question more clearer:

class MyClass
{
public:
MyClass()
{
//Do something.
}
MyClass(int a, int b)
{
//I need to call the parameter-less constructor. How?
}

Thank you for your attention.

Regards
Warren

Yes Absolute!

int _a, _b;

MyClass()
{
_a = 10;
_b = 20;
}
MyClass(int a, int b)
{
_a = a;
_b = b;

MyClass(); // this->MyClass(); // more precisely
}

the result will be _a = 10, _b = 20 even you create an instance by
calling

MyClass * _pClass = new MyClass(100, 100);

cheers,
 
I

Ian Collins

Alexander said:
Yes Absolute!
Absolutely no!
int _a, _b;

MyClass()
{
_a = 10;
_b = 20;
}
MyClass(int a, int b)
{
_a = a;
_b = b;

MyClass(); // this->MyClass(); // more precisely
}
Let's tidy that up a bit and define the class:

struct MyClass
{
int a, b;

MyClass()
: a(10), b(20)
{
a = 10;
b = 20;
}

MyClass(int a, int b)
: a(a), b(b)
{
MyClass();
}
};

All the statement "MyClass();" in the second constructor is create a new
default initialised MyClass. So you can call another MyClass
constructor, but that creates a new instance. It most definitely is not
the same as this->MyClass();

If we were to write

MyClass(int a, int b)
: a(a), b(b)
{
this->MyClass();
}

We would have a syntax error.
the result will be _a = 10, _b = 20 even you create an instance by
calling

MyClass * _pClass = new MyClass(100, 100);
No, it will not.
 
K

kasthurirangan.balaji

Hello,

Does C++ allow a constructor to call another constructor of the same
class? (I am focusing on the language abilities. I know the C# language
allow this.)

The following sample can explain this question more clearer:

class MyClass
{
public:
        MyClass()
        {
                //Do something.
        }
        MyClass(int a, int b)
        {
                //I need to call the parameter-less constructor. How?
        }

Thank you for your attention.

Regards
Warren

If your intention is to //Do something that is useful, you may very
well wrap that up in a member function like Init and call it from
either of the constructors.
IMHO, it isn't c++ way to call the default constructor from
parameterized constructor, the point being construction of objects.

Thanks,
Balaji.
 
A

Alexander Dong Back Kim

Absolutely no!




Let's tidy that up a bit and define the class:

struct MyClass
{
int a, b;

MyClass()
: a(10), b(20)
{
a = 10;
b = 20;
}

MyClass(int a, int b)
: a(a), b(b)
{
MyClass();
}

};

All the statement "MyClass();" in the second constructor is create a new
default initialised MyClass. So you can call another MyClass
constructor, but that creates a new instance. It most definitely is not
the same as this->MyClass();

If we were to write

MyClass(int a, int b)
: a(a), b(b)
{
this->MyClass();
}

We would have a syntax error.



No, it will not.

Oh really?

Thanks for that, I've just got back from C# world... my bad
I apologize.

cheers,
 
J

James Kanze

Warren Tang wrote:
No, if you have common initialisation code, it can go in a
member function.

More precisely, not yet. A proposal for this has been accepted
for the next version of C++.

Note that in this case, the call 1) must be in the
initialization list (and not in the body of the constructor),
and 2) once the first constructor has finished, the object is
considered "completely constructed", i.e.:

C::C( ... )
: member1( ... )
, member2( ... )
{
throw something ; // will not call destructor...
}

c::C( ... )
: C( transformed .. )
{
throw something ; // will call destructor...
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top