 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Katja Casutt Guest
|
Posted: Sat Sep 27, 2003 1:08 am Post subject: Call a constructor within a constructor of the same class |
|
|
Hi
I have a class having to constructors:
// default constructor
MyClass::MyClass()
: myMember1(0), myMember2(false)
{}
// second constructor
MyClass::MyClass(int myMember3)
: myMember3(0)
{}
Now, I'd like to call the default constructor in the second constructor.
I tried:
// second constructor
MyClass::MyClass(int myMember3)
: MyClass(), myMember3(0)
{}
But it did not work.
How can I call the default constructor?
Thanks in adavnce.
BTW: For people knowing Java: I search the C++ call for "this();"
Katja
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dirk Feytons Guest
|
Posted: Sat Sep 27, 2003 2:12 pm Post subject: Re: Call a constructor within a constructor of the same clas |
|
|
Katja Casutt wrote:
[snip code]
| Quote: | Now, I'd like to call the default constructor in the second constructor.
I tried:
// second constructor
MyClass::MyClass(int myMember3)
: MyClass(), myMember3(0)
{}
But it did not work.
How can I call the default constructor?
|
Simple answer: you can't.
Solution: copy&paste the c'tor initializer list from the default c'tor
to the other c'tor. If you really want to avoid the redundancy in the
source code you can factor out that code in a private member function
and call it in the body of the c'tor. Note that isn't always possible;
if myMember1 is const for instance, you can only initialize it by means
of the c'tor initializer list.
A more knowledgeable person will have to provide the exact technical
details on why C++ doesn't allow this syntax. An intuitive explanation
could be that a c'tor is a special function with object construction as
its sole purpose. It's not like an ordinary function that you can call
whenever you want.
--
Dirk
(PGP keyID: 0x448BC5DD - http://www.gnupg.org - http://www.pgp.com)
..oO° "You got a pea on your head dear." -- Thumb Wars °Oo.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Sat Sep 27, 2003 2:19 pm Post subject: Re: Call a constructor within a constructor of the same clas |
|
|
Katja Casutt wrote:
| Quote: | Now, I'd like to call the default constructor in the second constructor.
I tried:
// second constructor
MyClass::MyClass(int myMember3)
: MyClass(), myMember3(0)
{}
But it did not work.
How can I call the default constructor?
|
You can't. Anyway, don't treat ctors/dtors as functions, they are something
different and special. I personally don't say that they get called but
that they get invoked, to make the difference clear, but that is just my
personal taste.
In case you just want to share the init-code, one common way is to move it
to a common init() method that gets called by both ctors. In order to
Uli
--
Questions ?
see C++-FAQ Lite: http://parashift.com/c++-faq-lite/ first !
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Sat Sep 27, 2003 2:26 pm Post subject: Re: Call a constructor within a constructor of the same clas |
|
|
In article <e343edc3.0309252322.480eb0fe (AT) posting (DOT) google.com>, Katja
Casutt <casutt (AT) imt (DOT) ch> writes
| Quote: | Hi
I have a class having to constructors:
// default constructor
MyClass::MyClass()
: myMember1(0), myMember2(false)
{}
// second constructor
MyClass::MyClass(int myMember3)
: myMember3(0)
{}
Now, I'd like to call the default constructor in the second constructor.
I tried:
// second constructor
MyClass::MyClass(int myMember3)
: MyClass(), myMember3(0)
{}
But it did not work.
How can I call the default constructor?
Thanks in adavnce.
BTW: For people knowing Java: I search the C++ call for "this();"
|
In C++ you cannot. This may change in the future as I have a proposal
for change for the next release of the C++ Standard.
--
Francis Glassborow ACCU
If you are not using up-to-date virus protection you should not be reading
this. Viruses do not just hurt the infected but the hole community.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dhruv Guest
|
Posted: Sun Sep 28, 2003 1:29 am Post subject: Re: Call a constructor within a constructor of the same clas |
|
|
On Fri, 26 Sep 2003 21:08:18 -0400, Katja Casutt wrote:
| Quote: | Hi
I have a class having to constructors:
// default constructor
MyClass::MyClass()
: myMember1(0), myMember2(false)
{}
// second constructor
MyClass::MyClass(int myMember3)
: myMember3(0)
{}
Now, I'd like to call the default constructor in the second constructor.
I tried:
// second constructor
MyClass::MyClass(int myMember3)
: MyClass(), myMember3(0)
{}
|
You can try this, but many ppl will complain about this, because it may
not be standard compliant. What you do here is you first destroy the
current object, and the reconstruct at the same memory location.
MyClass::MyClass(int myMember3)
{
MyClass *temp = this;
this->~MyClass(); //Destroying possibly unconatructed object.
new (temp) MyClass;
//Any other initialization goes here.
}
But why would you want to do this anyways?
Regards,
-Dhruv.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Aaron Bentley Guest
|
Posted: Sun Sep 28, 2003 1:55 am Post subject: Re: Call a constructor within a constructor of the same clas |
|
|
Dirk Feytons wrote:
| Quote: | Katja Casutt wrote:
[snip]
How can I call the default constructor?
Simple answer: you can't.
[snip]
A more knowledgeable person will have to provide the exact technical
details on why C++ doesn't allow this syntax. An intuitive explanation
could be that a c'tor is a special function with object construction as
its sole purpose. It's not like an ordinary function that you can call
whenever you want.
|
There doesn't seem to be a technical reason not to-- the Standard will
likely be revised so you can do this.
Aaron
--
Aaron Bentley
www.aaronbentley.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Nikolai Borissov Guest
|
Posted: Sun Sep 28, 2003 2:02 am Post subject: Re: Call a constructor within a constructor of the same clas |
|
|
Katja Casutt wrote:
| Quote: | I have a class having to constructors:
// default constructor
MyClass::MyClass()
: myMember1(0), myMember2(false)
{}
// second constructor
MyClass::MyClass(int myMember3)
: myMember3(0)
{}
SNIP
How can I call the default constructor?
|
You can use placement new operator to achieve the effect of constructor
call, as it is shown in the example below.
Note, that I added initialization of myMember3 in the default constructor,
otherwise it is left uninitialized when we create an object through the
default constructor.
#include <new>
class MyClass
{
public:
MyClass():myMember1(0),myMember2(false),myMember3(0){}
MyClass(int myMember3):myMember3((new(this)MyClass,myMember3)){}
private:
int myMember1;
bool myMember2;
int myMember3;
};
Nikolai Borissov
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jakob Bieling Guest
|
Posted: Sun Sep 28, 2003 10:08 pm Post subject: Re: Call a constructor within a constructor of the same clas |
|
|
"Katja Casutt" <casutt (AT) imt (DOT) ch> wrote
| Quote: | Hi
I have a class having to constructors:
// default constructor
MyClass::MyClass()
: myMember1(0), myMember2(false)
{}
// second constructor
MyClass::MyClass(int myMember3)
: myMember3(0)
{}
Now, I'd like to call the default constructor in the second constructor.
I tried:
// second constructor
MyClass::MyClass(int myMember3)
: MyClass(), myMember3(0)
{}
But it did not work.
How can I call the default constructor?
|
You don't.
| Quote: | Thanks in adavnce.
BTW: For people knowing Java: I search the C++ call for "this();"
|
In C++ you cannot call c'tors. In that sense they are no functions.
Create an 'init' function that does the construction part that is common to
all c'tors and call that from your c'tors.
hth
--
jb
(replace y with x if you want to reply by e-mail)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ingolf Steinbach Guest
|
Posted: Tue Sep 30, 2003 6:43 pm Post subject: Re: Call a constructor within a constructor of the same clas |
|
|
Nikolai Borissov wrote:
| Quote: | #include
class MyClass
{
public:
MyClass():myMember1(0),myMember2(false),myMember3(0){}
MyClass(int myMember3):myMember3((new(this)MyClass,myMember3)){}
private:
int myMember1;
bool myMember2;
int myMember3;
};
|
IMO, you can easily run into various kinds of trouble with
this. For instance:
class BigArray {
public:
BigArray(); // allocates some megabytes on the heap
~BigArray(); // release the heap memory
// ...
};
class MyClass {
public:
MyClass() : myMember1(42), myMember2(7) {};
MyClass(int i); // calls MyClass default ctor directly
private:
BigArray array;
int myMember1;
int myMember2;
};
Here you would probably have a memory leak as array's ctor
would have been called twice and the heap memory allocation
from the first BigArray ctor call would be lost.
Kind regards
Ingolf
--
Ingolf Steinbach Jena-Optronik GmbH
[email]ingolf.steinbach (AT) jena-optronik (DOT) de[/email] ++49 3641 200-147
PGP: 0x7B3B5661 213C 828E 0C92 16B5 05D0 4D5B A324 EC04
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Nikolai Borissov Guest
|
Posted: Wed Oct 01, 2003 8:29 am Post subject: Re: Call a constructor within a constructor of the same clas |
|
|
Ingolf Steinbach wrote:
| Quote: |
IMO, you can easily run into various kinds of trouble with
this. For instance:
class BigArray {
public:
BigArray(); // allocates some megabytes on the heap
~BigArray(); // release the heap memory
// ...
};
class MyClass {
public:
MyClass() : myMember1(42), myMember2(7) {};
MyClass(int i); // calls MyClass default ctor directly
private:
BigArray array;
int myMember1;
int myMember2;
};
Here you would probably have a memory leak as array's ctor
would have been called twice and the heap memory allocation
from the first BigArray ctor call would be lost.
|
You are right, though this is not a point here. If constructor call was
implemented in C++ explicitly there still would be the same problem. The
point here is about a possibility to implement a functionality of a
constructor call through the placement new operator. And because this "call"
can be made from anywhere, there is no way to take all possible situations
into consideration and generalize. It's entirely up to the programmer to
watch for data integrity of an object being manipulated.
I know that many people don't like this technique even though it is legal
and non-ambiguous, because construction process is nothing more than:
first step - initialization of members in the order of their declaration;
second step - execution of a constructor code on initialized memory;
In your example, indeed, there will be a memory leak.
It's still possible to avoid a leak by calling BigArray object destructor at
some point, but this will result in allocation-deallocation-new_allocation
sequence, that may lead to wasted cycles (unless optimized out by compiler).
Here, specific design of BigArray is not particularly convenient for a
constructor call from another constructor.
In another situations, however, it may be necessary to reconstruct the
existing object on-the-fly. That's when a constructor call comes handy.
There might be a special Reconstruct method for this purpose that destructs
the object properly first and then calls one of the constructors.
Cheers,
Nikolai Borissov
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|