call a constructor

C

Chameleon

-------------------
class A {
public:
A() { blabla(); }
A(int a) : A() { another_blabla(); } // why wrong?
}
 
D

David Harmon

On Sat, 25 Nov 2006 02:51:21 +0200 in comp.lang.c++, Chameleon
I want 2nd ctor, runs content of 1st ctor and after its content.

You can only construct an object once. You can only use one constructor
for a given object.

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"# [10.3] Can one constructor of a class call another constructor of the
same class to initialize the this object?". It is always good to check
the FAQ before posting. You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/
 
A

Alf P. Steinbach

* Chameleon:
-------------------
class A {
public:
A() { blabla(); }
A(int a) : A() { another_blabla(); } // why wrong?
}
-------------------

I want 2nd ctor, runs content of 1st ctor and after its content.

In Java, I must use in first body line of A(int a) this:

You can

* Wait for the next version of the standard, C++0x.

* Use an "init" member function or functions (sometimes OK, sometimes
Evil) called from the constructor or constructor init list.

* Use an "init" member function called by client code after
construction. This idea, called two-phase construction, is
unspeakably evil and will cause you no ends of problems. But some
fail to recognize the connection with the problems they have (folks
doing embedded programs or old MFC are least ulikely to be blind
that way, because their tools almost force spaghetti on them).

* In some cases, simply use defaults for constructor arguments,
thus collapsing two or more constructors into one.

* Introduce an artificial base class, and place the common
constructor functionality there.

There is FAQ item on this, but last I checked that FAQ item was very
much less than complete in its coverage.
 
K

Kai-Uwe Bux

Chameleon said:
-------------------
class A {
public:
A() { blabla(); }
A(int a) : A() { another_blabla(); } // why wrong?
}

That's a no-no.
In Java, I must use in first body line of A(int a) this:

C++ is different.

-------------------
this();
-------------------
???


In C++?

E.g.:

class A {
public:
A() { blabla(); }
A(int a) { blabla(); another_blabla(); } // sic!
}


Best

Kai-Uwe Bux
 
S

Salt_Peter

Chameleon said:
-------------------
class A {
public:
A() { blabla(); }
A(int a) : A() { another_blabla(); } // why wrong?

because a ctor is not a composition member of the class. If you need
blabla() to be called, why not call it from another_blabla()?
}
-------------------

I want 2nd ctor, runs content of 1st ctor and after its content.

In Java, I must use in first body line of A(int a) this:

You can call functions in your init list. Hypotheticly, let blabla()
set a member and another_blabla() check validity:

#include <iostream>

class A {
int a;
public:
A() : a(blabla()) { }
A(int n) : a(another_blabla(n)) { }
int blabla() { init(); return 0; }
int another_blabla(int n) { init(); return (n < 0)?0:n; }
void init() { std::cout << "init\n"; }
};

int main()
{
A a;
A b(-1); // a = 0
A c(10);
}
 

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,778
Messages
2,569,605
Members
45,237
Latest member
AvivMNS

Latest Threads

Top