copy constructor

Started by
3 comments, last by Bregma 15 years, 8 months ago
hmm, im a bit confused by the following code
#include <iostream>
#include <string>
using namespace std;


class t
{
public:
	t(){		//default con
		cout << "default constructor" << endl;
	};
	 t(int in)	//int constructor
	{
		cout << "int constructor" << endl;
		this->mNum = in;
	};

	t(const t& in)	//copy constructor
	{
		cout << "copy constructor" << endl;
		this->mNum = in.mNum;
	};

	int mNum;
};

int main()
{
	t a;
	
	t b = 78;
	
	t c = b;
	

}

The way i see it, and the way my book describes (C++ Primer 4th ed), the call "t b = 78" , would result in t b = t(78); which in my mind would be a constructor call to the int constructor to create the temp obect containing the mNum 78, and then a call to the copy constructor with the that temp object, which gets copied to b. Such that i should get a copy constructor call aswell for the line "t b = 78;" and not just the line "t c = b" But the compiler just shows a constructor call for "t b = 78;" but i dont see how this is just done with one call,
Advertisement
What you are observing is an "implicit conversion". Implicit conversions are provided by single argument constructors.

Consider this example:
class Foo{    ...public:    Foo(int value){...}};...Foo f1(5);Foo f2 = 5;

Both those lines call the constructor Foo::Foo(int), the second version being the implicit conversion syntax.

To disable implicit conversions, precede the constructor with the explicit keyword.

For example:

class Foo{    ...public:    explicit Foo(int value){...}};...Foo f1(5);Foo f2 = 5; // ERROR, implicit int->Foo conversion disallowed
In at least some cases, the compiler is allowed to eliminate assignments and copy-constructions that it can prove are redundant. But more importantly, initializations don't follow the same rules as assignment statements: a line like 't b = 78;' is actually syntactic sugar for 't b(78);', except that it requires the constructor in question not to be explicit. Similarly, 't c = b;' is syntactic sugar for 't c(b);', and it just directly calls the copy constructor. By contrast, however, something like 't d; d = c;' uses the assignment operator in the second statement (and of course a default constructor in the first), not the copy constructor.
So basically...

The copy constructor should be called in the line

object b = 78;

ie

construct temp object - object(78)
copy con - b(object(78))

but if the compiler uses optimization and can get away with implicitly calling the constructor with the rhs instead, it will?
Quote:Original post by wforl
So basically...

The copy constructor should be called in the line

object b = 78;

No, the language requires that the int constructor be called, full stop. It is not allowed to create a temporary object using the int constructor then create another object using the copy constructor and then destroy the first object. That would be silly.

Like the above gentlemen from Toronto said, that particular initialization syntax is just syntactic sugar for proper initialization syntax.
T b = 6;
is exactly the same, semantically, as
T b(6)
, except that rules on implicit conversions are applicable. It is not an assignment statement. There is not copying or assignment involved.

Stephen M. Webb
Professional Free Software Developer

This topic is closed to new replies.

Advertisement