java like constructor calling constructor

L

lallous

Hello,

Probably I am not seeing it now, but is it possible for constructor(int) to
call constructor() ?

class A
{
private:
int x, y;
public:
A();
A(int);
};

A::A()
{
x = y = 0;
}

A::A(int x)
{
// call A::A() ?
// in Java, it is: this()
}

Or I have to write a small function like:
A::ctor()
{
// do common stuff here
}
A::A()
{
ctor();
}
A::A(int x)
{
ctor();
// more stuff here
}
 
A

Attila Feher

lallous said:
Hello,

Probably I am not seeing it now, but is it possible for
constructor(int) to call constructor() ?
[SNIP]

No. It is being discussed as a possible future feature (delegating
constructor???) but right now all you can do is to make a 3rd (named)
function and call that one from both constructors. This - of course - lacks
the capability of sharing initializer lists.
 
V

Victor Bazarov

lallous said:
Probably I am not seeing it now, but is it possible for constructor(int) to
call constructor() ?

No. Constructors don't have names, they cannot be called.
[...]

Or I have to write a small function like:
A::ctor()

Yes.

V
 
R

Rolf Magnus

lallous said:
Hello,

Probably I am not seeing it now, but is it possible for
constructor(int) to call constructor() ?
No.

Or I have to write a small function like:

Either that, or use default values for the parameters of your A(int)
constructor.
 
M

Martijn Lievaart

lallous said:
Hello,

Probably I am not seeing it now, but is it possible for
constructor(int) to call constructor() ?
[SNIP]

No. It is being discussed as a possible future feature (delegating
constructor???) but right now all you can do is to make a 3rd (named)
function and call that one from both constructors. This - of course - lacks
the capability of sharing initializer lists.

Well another thing that sometimes works is creating a baseclass that has
the common code in its constructor.

HTH,
M4
 
D

David Harmon

is it possible for constructor(int) to call constructor() ?

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/
 

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

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top