Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Constructors that call other Constructors

Reply
Thread Tools

Constructors that call other Constructors

 
 
Dave Rudolf
Guest
Posts: n/a
 
      02-05-2004
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


 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      02-05-2004
On Wed, 4 Feb 2004 20:38:16 -0600, "Dave Rudolf" <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
 
Dan Cernat
Guest
Posts: n/a
 
      02-05-2004

"Dave Rudolf" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
Jonathan Turkanis
Guest
Posts: n/a
 
      02-05-2004
"Dave Rudolf" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
Peter Koch Larsen
Guest
Posts: n/a
 
      02-05-2004

"Dan Cernat" <(E-Mail Removed)> skrev i en meddelelse
news:(E-Mail Removed)...
>
> "Dave Rudolf" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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.


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-05-2004
"Peter Koch Larsen" <(E-Mail Removed)> wrote...
>
> "Dan Cernat" <(E-Mail Removed)> skrev i en meddelelse
> news:(E-Mail Removed)...
> > 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?


 
Reply With Quote
 
Dan Cernat
Guest
Posts: n/a
 
      02-05-2004

"Peter Koch Larsen" <(E-Mail Removed)> wrote in message
news:fotUb.85255$(E-Mail Removed) k...
>
> "Dan Cernat" <(E-Mail Removed)> skrev i en meddelelse
> news:(E-Mail Removed)...
> >
> > "Dave Rudolf" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > 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


 
Reply With Quote
 
Peter Koch Larsen
Guest
Posts: n/a
 
      02-05-2004

"Victor Bazarov" <(E-Mail Removed)> skrev i en meddelelse
news:WguUb.98813$U%5.488957@attbi_s03...
> "Peter Koch Larsen" <(E-Mail Removed)> wrote...
> >
> > "Dan Cernat" <(E-Mail Removed)> skrev i en meddelelse
> > news:(E-Mail Removed)...
> > > 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




 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-05-2004
"Peter Koch Larsen" <(E-Mail Removed)> wrote...
>
> "Victor Bazarov" <(E-Mail Removed)> skrev i en meddelelse
> news:WguUb.98813$U%5.488957@attbi_s03...
> > "Peter Koch Larsen" <(E-Mail Removed)> wrote...
> > >
> > > "Dan Cernat" <(E-Mail Removed)> skrev i en meddelelse
> > > news:(E-Mail Removed)...
> > > > 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


 
Reply With Quote
 
Dave Rudolf
Guest
Posts: n/a
 
      02-06-2004
> (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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
reg constructors/copy constructors inheritance srp113 C++ 3 02-17-2009 04:01 PM
Is the possible to have all the public constructors of the publicbase class as the constructors of a derived class? Peng Yu C++ 5 09-19-2008 10:19 AM
compiler synthesized constructors/copy constructors/assignment operators Jess C++ 5 06-07-2007 11:09 AM
Copy constructors, de/constructors and reference counts Jeremy Smith C++ 2 08-02-2006 11:25 PM



Advertisments