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

another bloody basic question

In java, one constructor can call another constructor through this(...)

for instance

class foo
{
public:
foo(int k) { this(k,false)};
foo(int k, boolean m){...};
}

It seems i can't find similar syntax in c++.
what is the notion for one constructor to call another?

sorry for this bloody basic question.

jesse

Jul 19 '05 #1
7 3105
jesse wrote:
what is the notion for one constructor to call another?


The short answer is you cannot do it directly. Take a look at:
http://www.parashift.com/c++-faq-lit....html#faq-10.3

I think bookmarking the entire FAQ would be a good idea.

Regards,
Janusz

Jul 19 '05 #2
jesse wrote:
In java, one constructor can call another constructor through this(...)

for instance

class foo
{
public:
foo(int k) { this(k,false)};
foo(int k, boolean m){...};
}

It seems i can't find similar syntax in c++.
what is the notion for one constructor to call another?

sorry for this bloody basic question.
...


There's no such syntax in C++. In some cases the common code can be
transferred from constructors to a separate function (regular function,
not a constructor), which then can be called from all constructors. In
other cases there is no other way but to repeat the same code in all
constructors.

--
Best regards,
Andrey Tarasevich

Jul 19 '05 #3
On Wed, 05 Nov 2003 18:12:54 -0800, jesse <je*****@yahoo.com> wrote:
In java, one constructor can call another constructor through this(...)

for instance

class foo
{
public:
foo(int k) { this(k,false)};
foo(int k, boolean m){...};
}

It seems i can't find similar syntax in c++.
what is the notion for one constructor to call another?

sorry for this bloody basic question.


Basically you can't. The C++ construction model is very different to
Javas. By the time you enter the {} part of your constructor, all base
classes and all member variables have already been constructed. This
is because of the existence of user-defined value types, a concept
that Java doesn't have.

Have you come across initializer lists?

foo(int k)
:m_k(k)
{}

foo(int k, boolean m)
:m_k(k), m_m(m)
{}

With initializer lists, the constructor body is often empty anyway, so
what is there to share?

That said, I think a syntax like this has been proposed at some point
in the past, but I don't think any compiler supports it:

foo(int k)
:foo(k, false)
{
//anything extra
}

Tom
Jul 19 '05 #4
On Wed, 05 Nov 2003 18:12:54 -0800, jesse wrote:
In java, one constructor can call another constructor through this(...)

for instance

class foo
{
public:
foo(int k) { this(k,false)};
foo(int k, boolean m){...};
}

It seems i can't find similar syntax in c++.
what is the notion for one constructor to call another?


You can make an init() function that is called from the constructors,
though for your example the simplest would be:

class Foo {
public:
Foo(int k, bool m = false);
}

When you call do new Foo(42) the default is used for m since you didn't
supply a value.

--
NPV

"the large print giveth, and the small print taketh away"
Tom Waits - Step right up

Jul 19 '05 #5
"tom_usenet" <to********@hotmail.com> wrote in message
news:44********************************@4ax.com...
On Wed, 05 Nov 2003 18:12:54 -0800, jesse <je*****@yahoo.com> wrote:
In java, one constructor can call another constructor through this(...)

for instance

class foo
{
public:
foo(int k) { this(k,false)};
foo(int k, boolean m){...};
}

It seems i can't find similar syntax in c++.
what is the notion for one constructor to call another?

sorry for this bloody basic question.


Basically you can't. The C++ construction model is very different to
Javas. By the time you enter the {} part of your constructor, all base
classes and all member variables have already been constructed. This
is because of the existence of user-defined value types, a concept
that Java doesn't have.

Have you come across initializer lists?

foo(int k)
:m_k(k)
{}

foo(int k, boolean m)
:m_k(k), m_m(m)
{}

With initializer lists, the constructor body is often empty anyway, so
what is there to share?


The equivalent in C++ is to use the initializer list to indicate the
constructor of the base class to be used. It is selected by the normal
overloading operation.
Here is a simple example showing construction of a derived class with no,
one, or two arguments.

#include <iostream>
using namespace std;

class B
{public:
int b;
B( ):b(2){}
B(int x):b(x){}
};
class A : public B
{public:
int a;
A( ):a(1){}
A(int x):a(x){}
A(int x, int y):a(x),B(y){}
};
int main ( )
{
A myA;
cout <<myA.a<<myA.b<<endl;
A myA1(3);
cout<<myA1.a<<myA1.b<<endl;
A myA2(4,5);
cout << myA2.a<<myA2.b<<endl;
return 0;
}

--
Gary
Jul 19 '05 #6

"jesse" <je*****@yahoo.com> wrote in message news:3F**************@yahoo.com...
It seems i can't find similar syntax in c++.
what is the notion for one constructor to call another?


There is no similar syntax in C++. You can't call constructors in C++.
The cleanest work around is to put the common code in a seperate
member function and call it from both constructors.
Jul 19 '05 #7

"Andrey Tarasevich" <an**************@hotmail.com> wrote in message news:vq************@news.supernews.com...

There's no such syntax in C++. In some cases the common code can be
transferred from constructors to a separate function (regular function,
not a constructor), which then can be called from all constructors. In
other cases there is no other way but to repeat the same code in all
constructors.


There's nothing that ever prevents the constructor body from being
moved. What's a pain in the butt is that there is no way to share
the initializer lists between two constructors.
Jul 19 '05 #8

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

Similar topics

3
by: John | last post by:
Hello, I need to know which programs are opn on another machine in my network, these programs are opened from a shared drive on the server. How can I do this ? I've already dowloaded a sample...
3
by: Dany P. Wu | last post by:
Hi everyone, I am trying to create a site with pages containing iframes. This is the first time I've used inline frames and am still struggling with the concept. This site will have 10 pages...
188
by: christopher diggins | last post by:
I have posted a C# critique at http://www.heron-language.com/c-sharp-critique.html. To summarize I bring up the following issues : - unsafe code - attributes - garbage collection -...
2
by: moonriver | last post by:
In a xml file, can we make reference to another xml file so that all contents of the latter xml file will be included into the first xml file? Had better give me an example for details.
6
by: Christian Blackburn | last post by:
Hi Gang, I can't seem to figure out how to create an object array in VB.NET 2003. In VB6 you would just select an object copy and paste it and that was that. I don't know why in the heck they...
14
by: Craig Buchanan | last post by:
If I have two custom vb.net classes, where 80% of the properties are alike and there is one method with a matching signature, can i cast between one and the other? do i need to have each class...
38
by: Neo Geshel | last post by:
I am seeking a method to load one JS file directly into another, *without* having to dynamically write <scripttags. Is there any method whereby I can call only one external JS file using a ...
20
by: Johan | last post by:
How can I include one XML file into another XML file (on the client side, in Firefox)? I think XInclude is just what I need, but Firefox doesn't support it:...
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: 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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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.