473,624 Members | 2,346 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1892
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.po l.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.p ol.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.po l.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
9626
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 populate with a reference to the object that constructed this current object. i would attempt to accomplish this by setting the appropriate field from within the constructor... i figured it might be obtainable from the stack trace, but that...
9
27748
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){ day_ = d; month_ = m; year_ = y; secondo_ = se;
2
1907
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 pointer to the child class and call the function on_mdiactivate() using this pointer. For some reason, the program executes MDIChildClass::on_mdiactivate() and not Document::on_mdiactivate(). Why? on_mdiactivate() is a virtual function in...
4
7275
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 constructor again. In my larger program, I find that the member variables don't get re-initialized when "reconstructed". I don't have that problem in this demo, but the second time the constructor is called, its "this" points to a different location. ...
6
2102
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 (presuming the base constructor took an int): Derived::Derived(int a) : Base(a)
8
1583
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. class A { public A() {
12
5794
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. The subclass's constructor sets this variable to a real value. So first the variable is set to 0, the variable is never used before it once again gets set to a real value. It's a waste. Any ideas anyone?
6
2457
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 (the map constructor calls), saying that I'm trying to take the address of a constructor. Another compiler complains about all four of the last lines, just saying "invalid use of class". What is the correct syntax for calling a container...
1
8513
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 would be great if the following practice is valid. But by OO concept, it seems not to very good because init() belongs to an object, which hasn't be constructed. Can you give any comment on my question?
7
2675
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 name based on the name of the VB6 application. A second choice would be a file name based on the # COM interface assembly. I have tried calling Assembly.GetCallingAssembly() but this fails when I use the VB6 client. Is there a way to get this...
0
8233
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8170
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8675
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8334
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8474
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6108
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4078
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2604
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.