C++ Constructor initializer lists

Started by
2 comments, last by DigitalDelusion 18 years, 9 months ago

class CColor
{
public:
	unsigned int color;

	CColor( const CColor& c)
	{
		color = c.color;
	}
};

class A
{
public:
	CColor color;

	A(CColor nc)
		:	color( CColor(nc) )
	{

	}
};


Does this actually invoke the CColor's copy constructor twice? Should it be simply "color( nc )"?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Advertisement
Quote:Original post by Endar
Does this actually invoke the CColor's copy constructor twice? Should it be simply "color( nc )"?


Correct.
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
Actually, I think it copies it 3 times? Once to pass the argument because it isn't by reference . . .
Quote:Original post by Squirm
Actually, I think it copies it 3 times? Once to pass the argument because it isn't by reference . . .


depends on where you start counting ;) I just counted the initializer list, if you also count the call to A's ctor then you get three indeed.

HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats

This topic is closed to new replies.

Advertisement