Jump to content

View more

Image of the Day

Boxes as reward for our ranking mode. ヾ(☆▽☆)
#indiedev #gamedev #gameart #screenshotsaturday https://t.co/ALF1InmM7K
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

Chaining constructors in c++

4: Adsense

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   

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

#2 SiCrane   Moderators   

11761
Like
0Likes
Like

Posted 09 April 2009 - 08:20 AM

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

#3 htcoles   Members   

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   

1788
Like
0Likes
Like

Posted 09 April 2009 - 09:30 AM

clicky

#5 SiCrane   Moderators   

11761
Like
0Likes
Like

Posted 09 April 2009 - 09:34 AM

However see also this.

#6 megamoscha   Members   

657
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   

1788
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   

657
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   Members   

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   

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   Members   

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.