By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,897 Members | 1,969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,897 IT Pros & Developers. It's quick & easy.

Calling another constructor from a constructor

P: n/a
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
--
http://www.it-is-truth.org/
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
static void InitFunc(int n)
{
...
}

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

--
Buboi
=============================
??????????
"Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in message
news:c8**********@news8.svr.pol.co.uk...
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
--
http://www.it-is-truth.org/

Jul 22 '05 #2

P: n/a
"Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in message
news:c81ps3
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) { }
};
Jul 22 '05 #3

P: n/a
Buboi Peng wrote:
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.

--
http://www.it-is-truth.org/
Jul 22 '05 #4

P: n/a
Asfand Yar Qazi <im_not_giving_it_here@i_hate_spam.com> wrote in message news:<c8**********@news8.svr.pol.co.uk>...
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

Thanks,
Asfand Yar

Jul 22 '05 #5

P: n/a
On Fri, 14 May 2004 07:49:25 +0100 in comp.lang.c++, Asfand Yar Qazi
<im_not_giving_it_here@i_hate_spam.com> wrote,
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/

Jul 22 '05 #6

P: n/a

"Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in message
news:c8**********@news8.svr.pol.co.uk...

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.
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.