473,320 Members | 1,861 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,320 software developers and data experts.

Does C++ allow a constructor to call another constructor of the sameclass?

Hello,

Does C++ allow a constructor to call another constructor of the same
class? (I am focusing on the language abilities. I know the C# language
allow this.)

The following sample can explain this question more clearer:

class MyClass
{
public:
MyClass()
{
//Do something.
}
MyClass(int a, int b)
{
//I need to call the parameter-less constructor. How?
}

Thank you for your attention.

Regards
Warren
Jun 27 '08 #1
5 4840
I found this:
http://www.parashift.com/c++-faq-lit....html#faq-10.3
which states that it's not possible.
Warren Tang wrote:
Hello,

Does C++ allow a constructor to call another constructor of the same
class? (I am focusing on the language abilities. I know the C# language
allow this.)

The following sample can explain this question more clearer:

class MyClass
{
public:
MyClass()
{
//Do something.
}
MyClass(int a, int b)
{
//I need to call the parameter-less constructor. How?
}

Thank you for your attention.

Regards
Warren
Jun 27 '08 #2
Warren Tang wrote:
Hello,

Does C++ allow a constructor to call another constructor of the same
class?
No, if you have common initialisation code, it can go in a member function.

--
Ian Collins.
Jun 27 '08 #3
Thank you.

Ian Collins wrote:
Warren Tang wrote:
>Hello,

Does C++ allow a constructor to call another constructor of the same
class?

No, if you have common initialisation code, it can go in a member function.
Jun 27 '08 #4
On Apr 15, 7:21*am, Warren Tang <warren.c.t...@gmail.comwrote:
Hello,

Does C++ allow a constructor to call another constructor of the same
class? (I am focusing on the language abilities. I know the C# language
allow this.)

The following sample can explain this question more clearer:

class MyClass
{
public:
* * * * MyClass()
* * * * {
* * * * * * * * //Do something.
* * * * }
* * * * MyClass(int a, int b)
* * * * {
* * * * * * * * //I need to call the parameter-less constructor. How?
* * * * }

Thank you for your attention.

Regards
Warren
If your intention is to //Do something that is useful, you may very
well wrap that up in a member function like Init and call it from
either of the constructors.
IMHO, it isn't c++ way to call the default constructor from
parameterized constructor, the point being construction of objects.

Thanks,
Balaji.
Jun 27 '08 #5
On Apr 15, 4:32 am, Ian Collins <ian-n...@hotmail.comwrote:
Warren Tang wrote:
Does C++ allow a constructor to call another constructor of
the same class?
No, if you have common initialisation code, it can go in a
member function.
More precisely, not yet. A proposal for this has been accepted
for the next version of C++.

Note that in this case, the call 1) must be in the
initialization list (and not in the body of the constructor),
and 2) once the first constructor has finished, the object is
considered "completely constructed", i.e.:

C::C( ... )
: member1( ... )
, member2( ... )
{
throw something ; // will not call destructor...
}

c::C( ... )
: C( transformed .. )
{
throw something ; // will call destructor...
}

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: tzach | last post by:
Call constructor on an already existing instance? I like to execute the constructor logic, including all the class member constructors, on an existing instance (with out executing the destructor)....
5
by: lallous | last post by:
Hello, Probably I am not seeing it now, but is it possible for constructor(int) to call constructor() ? class A { private: int x, y; public:
3
by: S³awek | last post by:
Can one constructor of an object call another constructor of the same class? class foo { foo(float f, int i) // a "full" constructor { ... } foo(int i) // a "simplified" constructor {
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
1
by: mwebel | last post by:
Hi ikind of remember this being easy but cant find it on google: i have a base class and a derived class. The base class has two constructors normal and int overloaded one. thing is i want the...
9
by: toton | last post by:
Hi, Which one is the correct syntax for constructor for static memory allocation Object obj("param"); or Object obj = Object("param"); 1) For the first one, how compiler checks it as not a...
7
by: dragoncoder | last post by:
Hello experts, I have the following code me. =cat mystring.h #include<iostream> using namespace std; class mystring {
8
by: xtrigger303 | last post by:
Hi to all, I'm working on a smart pointer implementation and I'm trying to get automatic type conversion between different pointer types. I stumbled upon something weird (at least for me) I...
7
by: cppquester | last post by:
What does this code do? #include <iostream> class A { public: A() { std::cout << "A::A()" << std::endl;} };
6
by: Author | last post by:
I have class BaseClass { public BaseClass(string s1, string s2) { this.S1 = s1; this.S2 = s2; } public string S1 { get; set;}
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.