Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Calling another constructor from a constructor

Reply
Thread Tools

Calling another constructor from a constructor

 
 
Asfand Yar Qazi
Guest
Posts: n/a
 
      05-14-2004
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/
 
Reply With Quote
 
 
 
 
Buboi Peng
Guest
Posts: n/a
 
      05-14-2004
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:c81ps3$tbg$(E-Mail Removed)...
> 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/



 
Reply With Quote
 
 
 
 
Siemel Naran
Guest
Posts: n/a
 
      05-14-2004
"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) { }
};


 
Reply With Quote
 
Asfand Yar Qazi
Guest
Posts: n/a
 
      05-14-2004
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/
 
Reply With Quote
 
Christian
Guest
Posts: n/a
 
      05-14-2004
Asfand Yar Qazi <im_not_giving_it_here@i_hate_spam.com> wrote in message news:<c81ps3$tbg$(E-Mail Removed)>...
> 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

 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      05-14-2004
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/

 
Reply With Quote
 
jeffc
Guest
Posts: n/a
 
      05-17-2004

"Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in message
news:c81ps3$tbg$(E-Mail Removed)...
>
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Calling Class Constructor from Another Constructor Alf P. Steinbach C++ 4 11-30-2007 09:56 PM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Calling constructor inside another constructor Neroku Java 8 11-18-2006 10:19 AM
calling a constructor within a constructor Brett Irving C++ 3 06-29-2003 10:43 AM
why it's not possible calling constructor from constructor? Giulio C++ 9 06-25-2003 03:56 PM



Advertisments