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

call a constructor

-------------------
class A {
public:
A() { blabla(); }
A(int a) : A() { another_blabla(); } // why wrong?
}
-------------------

I want 2nd ctor, runs content of 1st ctor and after its content.

In Java, I must use in first body line of A(int a) this:

-------------------
this();
-------------------

In C++?

thanks
Nov 25 '06 #1
4 1598
On Sat, 25 Nov 2006 02:51:21 +0200 in comp.lang.c++, Chameleon
<ch******@hotmail.comwrote,
>I want 2nd ctor, runs content of 1st ctor and after its content.
You can only construct an object once. You can only use one constructor
for a given 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/

Nov 25 '06 #2
* Chameleon:
-------------------
class A {
public:
A() { blabla(); }
A(int a) : A() { another_blabla(); } // why wrong?
}
-------------------

I want 2nd ctor, runs content of 1st ctor and after its content.

In Java, I must use in first body line of A(int a) this:

-------------------
this();
-------------------

In C++?
You can

* Wait for the next version of the standard, C++0x.

* Use an "init" member function or functions (sometimes OK, sometimes
Evil) called from the constructor or constructor init list.

* Use an "init" member function called by client code after
construction. This idea, called two-phase construction, is
unspeakably evil and will cause you no ends of problems. But some
fail to recognize the connection with the problems they have (folks
doing embedded programs or old MFC are least ulikely to be blind
that way, because their tools almost force spaghetti on them).

* In some cases, simply use defaults for constructor arguments,
thus collapsing two or more constructors into one.

* Introduce an artificial base class, and place the common
constructor functionality there.

There is FAQ item on this, but last I checked that FAQ item was very
much less than complete in its coverage.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 25 '06 #3
Chameleon wrote:
-------------------
class A {
public:
A() { blabla(); }
A(int a) : A() { another_blabla(); } // why wrong?
}
-------------------

I want 2nd ctor, runs content of 1st ctor and after its content.
That's a no-no.
In Java, I must use in first body line of A(int a) this:
C++ is different.

-------------------
this();
-------------------
???

In C++?
E.g.:

class A {
public:
A() { blabla(); }
A(int a) { blabla(); another_blabla(); } // sic!
}
Best

Kai-Uwe Bux
Nov 25 '06 #4

Chameleon wrote:
-------------------
class A {
public:
A() { blabla(); }
A(int a) : A() { another_blabla(); } // why wrong?
because a ctor is not a composition member of the class. If you need
blabla() to be called, why not call it from another_blabla()?
}
-------------------

I want 2nd ctor, runs content of 1st ctor and after its content.

In Java, I must use in first body line of A(int a) this:

-------------------
this();
-------------------

In C++?
You can call functions in your init list. Hypotheticly, let blabla()
set a member and another_blabla() check validity:

#include <iostream>

class A {
int a;
public:
A() : a(blabla()) { }
A(int n) : a(another_blabla(n)) { }
int blabla() { init(); return 0; }
int another_blabla(int n) { init(); return (n < 0)?0:n; }
void init() { std::cout << "init\n"; }
};

int main()
{
A a;
A b(-1); // a = 0
A c(10);
}

Nov 25 '06 #5

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

Similar topics

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) .
8
by: trying_to_learn | last post by:
Why do we need to explicitly call the copy constructor and the operator = , for base class and member objects in composition? ....book says "You must explicitly call the GameBoard copy-constructor...
4
by: Greg | last post by:
Is it possible to call a constructor from within a constructor I mean Class A { public A(string getModifiedVal) { .........
18
by: AlexanderVX | last post by:
How do I write a constructor mehtod call in this case /*-----------*/ template<typename Tclass CObjectPoolImpl { public: void smth(T* pObj) { if (pObj)
13
by: shsingh | last post by:
I have a class A containing some map as data variables. I creat an object of class A on heap by allocatiing memory by using "malloc". This will return me the required memory but the object is not...
7
by: dragoncoder | last post by:
Hello experts, I have the following code me. =cat mystring.h #include<iostream> using namespace std; class mystring {
7
by: cppquester | last post by:
What does this code do? #include <iostream> class A { public: A() { std::cout << "A::A()" << std::endl;} };
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
15
by: asm23 | last post by:
Hi, everyone, I'm studying the <<Thinking in C++>volume Two. In Chapter One, the example code : Auto_ptr.cpp //------------------------------------------------------- #include <memory> #include...
1
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.