• Advertisement
Sign in to follow this  

Chaining constructors in c++

This topic is 3205 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I googled this topic, and apparently this is syntacticly allowed, but doesn't work properly, I'm wondering if I can get some confirmation of this.

Share this post


Link to post
Share on other sites
Advertisement
That depends on what you mean by "chaining constructors".

Share this post


Link to post
Share on other sites
having a constructor (say a no-arguement constructor) internally call another constructor to avoid code duplication. I'll give an example.

class Point2D
{

float x,y;

public:

Point2D(float x, float y)
{
this->x = x;
this->y = y;
}


Point2D()
{
this(0,0); //or Point2D(0,0) , i'm not sure whch c++ would use

}


};

Share this post


Link to post
Share on other sites
In your simple example you can give the parameters default values. e.g.


class Point
{
public:
float x,y;

Point(float x = 0.0f, float y = 0.0f)
: x(x), y(y){}
};


You could also use named constructors as in C++ FAQ lite explained or wait for C++0x. ^^

Share this post


Link to post
Share on other sites
Quote:
Original post by megamoscha
In your simple example you can give the parameters default values. e.g.


class Point
{
public:
float x,y;

Point(float x = 0.0f, float y = 0.0f)
: x(x), y(y){}
};

Careful there. That expands out to THREE constructors, taking from zero to two arguments. And the one-constructor one will act as an implicit conversion constructor. Which means that code like Point p(1,2); p = p + 3; will compile fine, and almost certainly do something you weren't expecting. As a rule of thumb, any constructors that can take exactly one argument should be declared explicit, unless you actually want that behavior.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
However see also this.


This technique can also be used in pre-0x C++, if you make a dummy base class (although in most cases it won't really save you any work):


struct PointBase {
float x, y;
PointBase(float x, float y): x(x), y(y) {}
};

struct Point: PointBase {
Point(float x, float y): PointBase(x, y) {}
Point(): PointBase(0, 0) {}
};

Share this post


Link to post
Share on other sites
thanks a lot for the help guys. *--- I believe(but I could be wrong) that some way of constructor chaining may be included in the C++ 0x specs, but since the website didn't mention a specific source, I can't confirm.--* - *EDIT* I should have read the improving construction from one of those links before i said this, as it mentions it as well

I've been trying to apply some of the techniques i've been learning in school to my c++ programming to clean my code up a little, and just generally adhere to better software engineering principles.

Share this post


Link to post
Share on other sites
BTW, the term you're looking for is probably something more like forwarding constructors. :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement