Calling another constructor from a constructor

A

Asfand Yar Qazi

Hi,

Consider this:

class C
{
int j;
public:
C(int i)
: j(i)
{
}

C()
{
//C::C(0)
// this is obviously wrong, but is
// something like this possible?

*this = C(0);
}

}; // class C

Is there any way of calling another constructor from a
constructor to initialize your object? I know about using the
assignment operator as a go-between as above, but is there some
other more efficient way?

Thanks,
Asfand Yar
 
B

Buboi Peng

static void InitFunc(int n)
{
...
}

class C{
friend void InitFunc();
public:
C(int n){ InitFunc(n); }
C() { InitFunc(0); }
};
 
S

Siemel Naran

class C
{
int j;
public:
C(int i)
: j(i)
{
}

C()
{
//C::C(0)
// this is obviously wrong, but is
// something like this possible?

*this = C(0);
}

}; // class C

It's a nice feature to have, one constructor forwards to another.

The usual ideas are:

(1) Use default parameters. In your case

C(int i=0)

(2) Have the constructors call one init function. But if you have user
types, this means you call the default constructor for each in the
(non-existent) member initialization list, though this might be OK for all
practical purposes.

(3) Define a nested class, and have your class contain an instance of this
class. The nested class constructor is the init function of method (1).

class C
{
struct Imp { int j; Imp(int j); } imp;
public:
C(int i) : imp(i) { }
C() : imp(0) { }
};
 
A

Asfand Yar Qazi

Buboi said:
static void InitFunc(int n)
{
...
}

class C{
friend void InitFunc();
public:
C(int n){ InitFunc(n); }
C() { InitFunc(0); }
};

I did something like this in the end.

Thanks to all.
 
C

Christian

Asfand Yar Qazi said:
Hi,

Consider this:

class C
{
int j;
public:
C(int i)
: j(i)
{
}

C()
{
//C::C(0)
// this is obviously wrong, but is
// something like this possible?

*this = C(0);
}

}; // class C

Is there any way of calling another constructor from a
constructor to initialize your object?

No. But as another poster suggested, you might use some sort of "init"
function to call from both.
I know about using the
assignment operator as a go-between as above, but is there some
other more efficient way?

For your particular example, why not simply use a single constructor
with a default argument (defaulting to 0 in your case)?

Cheers,
Christian
 
D

David Harmon

On Fri, 14 May 2004 07:49:25 +0100 in comp.lang.c++, Asfand Yar Qazi
Is there any way of calling another constructor from a
constructor to initialize your 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/
 
J

jeffc

Asfand Yar Qazi said:
Is there any way of calling another constructor from a
constructor to initialize your object?

No. You can just create an "initialize" method that they both call.
 

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,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top