Jump to content

  • Log In with Google      Sign In   
  • Create Account


Chaining constructors in c++


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 htcoles   Members   -  Reputation: 182

Like
0Likes
Like

Posted 09 April 2009 - 08:18 AM

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.
--------------------------------------Not All Martyrs See Divinity, But At Least You Tried

Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9387

Like
0Likes
Like

Posted 09 April 2009 - 08:20 AM

That depends on what you mean by "chaining constructors".

#3 htcoles   Members   -  Reputation: 182

Like
0Likes
Like

Posted 09 April 2009 - 09:28 AM

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

}


};
--------------------------------------Not All Martyrs See Divinity, But At Least You Tried

#4 Sneftel   Senior Moderators   -  Reputation: 1776

Like
0Likes
Like

Posted 09 April 2009 - 09:30 AM

clicky

#5 SiCrane   Moderators   -  Reputation: 9387

Like
0Likes
Like

Posted 09 April 2009 - 09:34 AM

However see also this.

#6 megamoscha   Members   -  Reputation: 571

Like
0Likes
Like

Posted 09 April 2009 - 10:35 AM

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. ^^


#7 Sneftel   Senior Moderators   -  Reputation: 1776

Like
0Likes
Like

Posted 09 April 2009 - 10:41 AM

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.

#8 megamoscha   Members   -  Reputation: 571

Like
0Likes
Like

Posted 09 April 2009 - 11:02 AM

Ah good point, never thought about that. I'm anyway for named constructors, because it makes it clear what is meant.

#9 Zahlman   Moderators   -  Reputation: 1682

Like
0Likes
Like

Posted 09 April 2009 - 03:19 PM

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) {}
};


#10 htcoles   Members   -  Reputation: 182

Like
0Likes
Like

Posted 09 April 2009 - 05:29 PM

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.
--------------------------------------Not All Martyrs See Divinity, But At Least You Tried

#11 Zahlman   Moderators   -  Reputation: 1682

Like
0Likes
Like

Posted 10 April 2009 - 01:30 AM

BTW, the term you're looking for is probably something more like forwarding constructors. :)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS