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

Constructors that call other Constructors

Hi all,

At the risk of mentioning Java in a C++ news group :), I'm curious if there
is a way to reuse constructors in C++ similar to what can be done in Java.
For instance, in java, I can do the following:

public class Rectangle
{
public Rectangle( int width, int height )
{
// Do stuff here
}

public Rectangle( int widthAndHeight )
{
this( widthAndHeight , widthAndHeight );
}
}

That is, the second constructor simply calls the first to do all of the
dirty work. Is there some similar concept in C++? Certainly, a direct
translation does not work.

Dave
Jul 22 '05 #1
12 1791
On Wed, 4 Feb 2004 20:38:16 -0600, "Dave Rudolf" <nu****************@hotmail.com> wrote:
At the risk of mentioning Java in a C++ news group :), I'm curious if there
is a way to reuse constructors in C++ similar to what can be done in Java.
For instance, in java, I can do the following:

public class Rectangle
{
public Rectangle( int width, int height )
{
// Do stuff here
}

public Rectangle( int widthAndHeight )
{
this( widthAndHeight , widthAndHeight );
}
}

That is, the second constructor simply calls the first to do all of the
dirty work. Is there some similar concept in C++? Certainly, a direct
translation does not work.


In C++ use a non-virtual member function e.g. called 'init'. Call that
from each constructor. You cannot do that safely in Java since all
methods in Java are virtual, so Java needs the "constructor reuse"; it's
not needed and therefore not supported in C++.

By the way, this is a FAQ,
<url: http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.3>.

You should always check the FAQ first before asking here.

Jul 22 '05 #2

"Dave Rudolf" <nu****************@hotmail.com> wrote in message
news:10*************@corp.supernews.com...
Hi all,

At the risk of mentioning Java in a C++ news group :), I'm curious if there is a way to reuse constructors in C++ similar to what can be done in Java.
For instance, in java, I can do the following:

public class Rectangle
{
public Rectangle( int width, int height )
{
// Do stuff here
}

public Rectangle( int widthAndHeight )
{
this( widthAndHeight , widthAndHeight );
}
}

That is, the second constructor simply calls the first to do all of the
dirty work. Is there some similar concept in C++? Certainly, a direct
translation does not work.

Dave


no, one cannot use constructors like you mentioned.
however, there are two solutions to your problem
1. use a common function
class Rectangle
{
private:
void DoStuff(int w, int h)
{
// do the stuff here
}
public:
Rectangle ( int width_and_height)
{
DoStuff(width_and_height, width_and_height);
}
Rectangle ( int w, int h)
{
DoStuff(w, h);
}
};

2. use default values
class Rectangle
{
public:
Rectangle ( int w, int h = GetTheDefaultheightValue())
{
// do stuff with w and h
}

private:
int GetTheDefaultheightValue()
{
rturn 42;
}
};

dan
Jul 22 '05 #3
"Dave Rudolf" <nu****************@hotmail.com> wrote in message
news:10*************@corp.supernews.com...
Hi all,

At the risk of mentioning Java in a C++ news group :), I'm curious if there is a way to reuse constructors in C++ similar to what can be done in Java. For instance, in java, I can do the following:

public class Rectangle
{
public Rectangle( int width, int height )
{
// Do stuff here
}

public Rectangle( int widthAndHeight )
{
this( widthAndHeight , widthAndHeight );
}
}

That is, the second constructor simply calls the first to do all of the dirty work. Is there some similar concept in C++? Certainly, a direct translation does not work.


There has been discussion of adding a mechanism like this to C++,
using the syntax for base initialization:

Rectangle(int widthAndHeight) : Rectangle(widthAndHeight ,
widthAndHeight ) { }

See http://anubis.dkuug.dk/jtc1/sc22/wg2...2002/n1395.txt.

In your example you could possibly do something like this:

Rectangle(int width, int height = -1)
: width_(width),
height_(height != -1 ? height : width)
{ }

(By the way, in C++ you might want to start thinking along these
lines:

Rectangle( int width, int height )
// Do stuff here
{
// Do stuff here
} )

Jonathan
Jul 22 '05 #4

"Dan Cernat" <ce****@dan.com> skrev i en meddelelse
news:10*************@corp.supernews.com...

"Dave Rudolf" <nu****************@hotmail.com> wrote in message
news:10*************@corp.supernews.com...
Hi all,

At the risk of mentioning Java in a C++ news group :), I'm curious if

there
is a way to reuse constructors in C++ similar to what can be done in Java. For instance, in java, I can do the following:

public class Rectangle
{
public Rectangle( int width, int height )
{
// Do stuff here
}

public Rectangle( int widthAndHeight )
{
this( widthAndHeight , widthAndHeight );
}
}

That is, the second constructor simply calls the first to do all of the
dirty work. Is there some similar concept in C++? Certainly, a direct
translation does not work.

Dave


no, one cannot use constructors like you mentioned.
however, there are two solutions to your problem
1. use a common function
class Rectangle
{
private:
void DoStuff(int w, int h)
{
// do the stuff here
}
public:
Rectangle ( int width_and_height)
{
DoStuff(width_and_height, width_and_height);
}
Rectangle ( int w, int h)
{
DoStuff(w, h);
}
};

2. use default values
class Rectangle
{
public:
Rectangle ( int w, int h = GetTheDefaultheightValue())
{
// do stuff with w and h
}

private:
int GetTheDefaultheightValue()
{
rturn 42;
}
};

dan


Well.... that second version is a little suspect, isn't it? calling a
memberfunction on a noninitialized object.
Jul 22 '05 #5
"Peter Koch Larsen" <pk*****@mailme.dk> wrote...

"Dan Cernat" <ce****@dan.com> skrev i en meddelelse
news:10*************@corp.supernews.com...
2. use default values
class Rectangle
{
public:
Rectangle ( int w, int h = GetTheDefaultheightValue())
{
// do stuff with w and h
}

private:
int GetTheDefaultheightValue()
{
rturn 42;
}
};

dan


Well.... that second version is a little suspect, isn't it? calling a
memberfunction on a noninitialized object.


What's noninitialized about 42?
Jul 22 '05 #6

"Peter Koch Larsen" <pk*****@mailme.dk> wrote in message
news:fo*********************@news000.worldonline.d k...

"Dan Cernat" <ce****@dan.com> skrev i en meddelelse
news:10*************@corp.supernews.com...

"Dave Rudolf" <nu****************@hotmail.com> wrote in message
news:10*************@corp.supernews.com...
Hi all,

At the risk of mentioning Java in a C++ news group :), I'm curious if

there
is a way to reuse constructors in C++ similar to what can be done in Java. For instance, in java, I can do the following:

public class Rectangle
{
public Rectangle( int width, int height )
{
// Do stuff here
}

public Rectangle( int widthAndHeight )
{
this( widthAndHeight , widthAndHeight );
}
}

That is, the second constructor simply calls the first to do all of the dirty work. Is there some similar concept in C++? Certainly, a direct
translation does not work.

Dave


no, one cannot use constructors like you mentioned.
however, there are two solutions to your problem
1. use a common function
class Rectangle
{
private:
void DoStuff(int w, int h)
{
// do the stuff here
}
public:
Rectangle ( int width_and_height)
{
DoStuff(width_and_height, width_and_height);
}
Rectangle ( int w, int h)
{
DoStuff(w, h);
}
};

2. use default values
class Rectangle
{
public:
Rectangle ( int w, int h = GetTheDefaultheightValue())
{
// do stuff with w and h
}

private:
int GetTheDefaultheightValue()
{
rturn 42;
}
};

dan


Well.... that second version is a little suspect, isn't it? calling a
memberfunction on a noninitialized object.


Hmmm, yes, you are right if GetTheDefaultheightValue() tries to use any
members of the object that is in process of constructing at that moment. I
used that example to emphasis the fact that the default arguments aren't
necessarely constants, but can be the result of a function call, as well.

dan
Jul 22 '05 #7

"Victor Bazarov" <v.********@comAcast.net> skrev i en meddelelse
news:WguUb.98813$U%5.488957@attbi_s03...
"Peter Koch Larsen" <pk*****@mailme.dk> wrote...

"Dan Cernat" <ce****@dan.com> skrev i en meddelelse
news:10*************@corp.supernews.com...
2. use default values
class Rectangle
{
public:
Rectangle ( int w, int h = GetTheDefaultheightValue())
{
// do stuff with w and h
}

private:
int GetTheDefaultheightValue()
{
rturn 42;
}
};

dan


Well.... that second version is a little suspect, isn't it? calling a
memberfunction on a noninitialized object.


What's noninitialized about 42?


Nothing. But look what I wrote. Even though the above construct looks quite
innocent and probably works on actual implementations, my guess is that it
is undefined behaviour if allowed at all.

Kind regards
Peter


Jul 22 '05 #8
"Peter Koch Larsen" <pk*****@mailme.dk> wrote...

"Victor Bazarov" <v.********@comAcast.net> skrev i en meddelelse
news:WguUb.98813$U%5.488957@attbi_s03...
"Peter Koch Larsen" <pk*****@mailme.dk> wrote...

"Dan Cernat" <ce****@dan.com> skrev i en meddelelse
news:10*************@corp.supernews.com...
> 2. use default values
> class Rectangle
> {
> public:
> Rectangle ( int w, int h = GetTheDefaultheightValue())
> {
> // do stuff with w and h
> }
>
> private:
> int GetTheDefaultheightValue()
> {
> rturn 42;
> }
> };
>
> dan
>

Well.... that second version is a little suspect, isn't it? calling a
memberfunction on a noninitialized object.
What's noninitialized about 42?


Nothing. But look what I wrote. Even though the above construct looks

quite innocent and probably works on actual implementations, my guess is that it
is undefined behaviour if allowed at all.


In this particular case, calling a _non_static_ member function is simply
not allowed, you're correct. But if we take our discussion into academics,
why do you say that it would be _undefined_behaviour_?

V
Jul 22 '05 #9
> (By the way, in C++ you might want to start thinking along these
lines:

Rectangle( int width, int height )
// Do stuff here
{
// Do stuff here
} )

Jonathan


Yes, I generally use initialization lists outside of the "code block". The
only exception is when the initialization requires some number crunching.
But I do this because I have been told that it is the right way to write
C++, although you have me curious as to why this is the preferred way to
initialize local fields. Is it an optimization thing ?

Dave
Jul 22 '05 #10
"Dave Rudolf" <nu****************@hotmail.com> wrote...
(By the way, in C++ you might want to start thinking along these
lines:

Rectangle( int width, int height )
// Do stuff here
{
// Do stuff here
} )

Jonathan


Yes, I generally use initialization lists outside of the "code block". The
only exception is when the initialization requires some number crunching.
But I do this because I have been told that it is the right way to write
C++, although you have me curious as to why this is the preferred way to
initialize local fields. Is it an optimization thing ?


It's covered in the FAQ. See 'Constructors'.

V
Jul 22 '05 #11

"Dave Rudolf" <nu****************@hotmail.com> wrote in message
news:10*************@corp.supernews.com...

Yes, I generally use initialization lists outside of the "code block". The only exception is when the initialization requires some number crunching. But I do this because I have been told that it is the right way to write C++, although you have me curious as to why this is the preferred way to initialize local fields. Is it an optimization thing ?


By the time you get to rthe body of the constructors, bases and
members have already been initialized. Sometime you are forced to
initialize a base or member explicitly in the initializer list, .e.g.
for const or reference members or types without default constructors
or assignment operators. Even when a type has a default constructor
and assignment operator, default initialization + assignment may not
have the same semantics as direct initialization, and is often less
efficient.

Jonathan
Jul 22 '05 #12
On Wed, 04 Feb 2004 20:38:16 -0600, Dave Rudolf wrote:
Hi all,

At the risk of mentioning Java in a C++ news group :), I'm curious if there
is a way to reuse constructors in C++ similar to what can be done in Java.
For instance, in java, I can do the following:

public class Rectangle
{
public Rectangle( int width, int height )
{
// Do stuff here
}

public Rectangle( int widthAndHeight )
{
this( widthAndHeight , widthAndHeight );
}
}

That is, the second constructor simply calls the first to do all of the
dirty work. Is there some similar concept in C++? Certainly, a direct
translation does not work.


Another technique is to factor out the common behaviour in a base class.
This may seem overkill at first, but in reality is surprisingly often
only an implementation of one-class, one-responsibility.

HTH,
M4

Jul 22 '05 #13

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

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
10
by: John | last post by:
Trying to find out what is essential / optional, I made an extremely simple Class and Module combination to add two numbers. (see below) It appears that an empty constructor is needed n order to...
3
by: John | last post by:
Before anything else, thanks Marina, Workgroups and Ralf, for your help so far. I am now able to better define the question! After adding more console printout lines to CSum, I tried all...
1
by: John | last post by:
Hi, Maybe someone can help me with the following: "The first task by any derived class constructor is to call it’s direct or indirect base class constructor implicitly or explicitly", reads the...
6
by: DeveloperX | last post by:
In an attempt to solve a problem further down I suggested the problem might be caused by a missing constructor. Now, this led me to the conclusion that I don't fully understand constructors, or...
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:
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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
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...
0
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...
0
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,...

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.