Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Does C++ allow a constructor to call another constructor of the sameclass?

Reply
Thread Tools

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

 
 
Warren Tang
Guest
Posts: n/a
 
      04-15-2008
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
 
Reply With Quote
 
 
 
 
Warren Tang
Guest
Posts: n/a
 
      04-15-2008
I found this:
http://www.parashift.com/c++-faq-lit....html#faq-10.3
which states that it's not possible.


Warren Tang wrote:
> 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

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      04-15-2008
Warren Tang wrote:
> 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.

--
Ian Collins.
 
Reply With Quote
 
Warren Tang
Guest
Posts: n/a
 
      04-15-2008
Thank you.

Ian Collins wrote:
> Warren Tang wrote:
>> 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.
>

 
Reply With Quote
 
Alexander Dong Back Kim
Guest
Posts: n/a
 
      04-15-2008
On Apr 15, 12:21 pm, Warren Tang <(E-Mail Removed)> wrote:
> 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,
 
Reply With Quote
 
Warren Tang
Guest
Posts: n/a
 
      04-15-2008
I've just read this:
http://www.parashift.com/c++-faq-lit....html#faq-10.3

That is a new object intuitively, so I don't like this way even the
compiler really has special support for it.

Anyway thanks for your comment.


Alexander Dong Back Kim wrote:
> On Apr 15, 12:21 pm, Warren Tang <(E-Mail Removed)> wrote:
>> 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,

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      04-15-2008
Alexander Dong Back Kim wrote:
>>
>> Does C++ allow a constructor to call another constructor of the same
>> class?

>
> 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.

--
Ian Collins.
 
Reply With Quote
 
kasthurirangan.balaji@gmail.com
Guest
Posts: n/a
 
      04-15-2008
On Apr 15, 7:21*am, Warren Tang <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
Alexander Dong Back Kim
Guest
Posts: n/a
 
      04-15-2008
On Apr 15, 1:41 pm, Ian Collins <(E-Mail Removed)> wrote:
> Alexander Dong Back Kim wrote:
>
> >> Does C++ allow a constructor to call another constructor of the same
> >> class?

>
> > 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.
>
> --
> Ian Collins.


Oh really?

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

cheers,
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-15-2008
On Apr 15, 4:32 am, Ian Collins <(E-Mail Removed)> wrote:
> Warren Tang wrote:


> > 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.


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...
}

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A Java "interface" declaration does not allow a constructor to be specified in it ... Casey Hawthorne Java 12 01-19-2010 02:37 AM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
How to call a constructor from within another constructor Andy C++ 9 09-24-2006 03:17 PM
Constructor question, how does the call to the parent constructor work? marcwentink@hotmail.com C++ 6 05-09-2006 07:19 AM
call null constructor from another constructor Matt Graham C++ 3 02-07-2004 08:41 AM



Advertisments