473,387 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Calling another constructor from a constructor

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
6 1877
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
"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
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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Murat Tasan | last post by:
i have a quick question... is there a way to obtain the reference to the object which called the currently executing method? here is the scenario, i have a class and a field which i would like to...
9
by: Giulio | last post by:
why definition of two constructors like these is not possible in c++??? ----------------------- date::date(const int d, const int m, const int y, const int ora, const int mi, const int se){...
2
by: William Payne | last post by:
Hello, consider these following two classes. A base class, class MDIChildWindow, and a class inherting from that base class, class Document. In the static base member function callback() I obtain a...
4
by: Jerry Krinock | last post by:
I've written the following demo to help me understand a problem I'm having in a larger program. The "main" function constructs a Foo object, and then later "reconstructs" it by calling the...
6
by: Justin | last post by:
Hello, first time posting. If I have a base class and a derived class, is there only one way to call the base constructor? i.e. Is this the only way I can call the base constructor...
8
by: Greg Bacchus | last post by:
I have a base class with a method that is to be called in the constructor of the inheritting classes. Is there any way of determining, say, the Type of the class that is calling it. e.g. ...
12
by: st_ev_fe | last post by:
I've noticed that when constructing a subclass, the base class get's it's contructors called. Is there some way to avoid this? The base class has one variable, which gets initialised to 0. ...
6
by: daveb | last post by:
I'm trying to write some code that calls the constructors of STL containers explicitly, and I can't get it to compile. A sample program is below. One compiler complains about the last two lines...
1
by: newbie | last post by:
This is a snippet from C++ FAQs, which I have never done--- when I do such a thing, I would declare function used by constructor ( in this example, init() ) as static. But I do understand that it...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.