473,791 Members | 2,985 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1624
On Sat, 25 Nov 2006 02:51:21 +0200 in comp.lang.c++, Chameleon
<ch******@hotma il.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_blabl a(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
2251
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
5184
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
2983
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 or the default constructor is automatically called instead" Why cant the compiler do this on its own. if we are making an object through copr construction for an inherited class , then why not simply call the corresponding copy constructors for...
4
2069
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
3668
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
9729
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 initialized properly as constructor same is not get called ( as per the behavior). How to call a constructor explicitly if we want to allocate memory using malloc ?
7
2714
by: dragoncoder | last post by:
Hello experts, I have the following code me. =cat mystring.h #include<iostream> using namespace std; class mystring {
7
1645
by: cppquester | last post by:
What does this code do? #include <iostream> class A { public: A() { std::cout << "A::A()" << std::endl;} };
12
7216
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? class Trial { public: Trial() {
15
1883
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 <iostream> #include <cstddef> using namespace std; class TraceHeap { int i;
0
9515
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
10207
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10155
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
9029
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
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
5431
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
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.