Copy Constructor and explicit attribute

J

Jean Stax

Hi !

I tried to understand when the explicit attribute in copy constructor
prevents from
me to create a new object. Bellow is the sample code.
While the two first cases really generate a compilation error, the
third (mc2 = Foo1();) compiles and runs without any problem. I am
wondering why the:

MyClass Foo1()
{
MyClass mc(1);
return mc;
}

isn't forbidden, when MyClass copy constructor is defined as explicit.

Thanks.


class MyClass
{
public:
MyClass(int nVal) : m_nVal1(nVal){}
MyClass() : m_nVal1(0){}
explicit MyClass(const MyClass& copy)
{
//...
}
private:
int m_nVal1;
};

void Foo(MyClass cc)
{
}

MyClass Foo1()
{
MyClass mc(1);
return mc;
}

int main(int argc, char* argv[])
{
MyClass mc(1);
MyClass mc1 = mc;
//error C2440: 'initializing' : cannot convert from 'class MyClass' to
//class MyClass' No copy constructor available for class 'MyClass'

Foo(mc);
//error C2664: 'Foo' : cannot convert parameter 1 from 'class MyClass'
//to 'class MyClass'No copy constructor available for class 'MyClass'

MyClass mc2;
mc2 = Foo1();


return 0;
}
 
L

lilburne

Jean said:
Hi !

I tried to understand when the explicit attribute in copy constructor
prevents from
me to create a new object. Bellow is the sample code.
While the two first cases really generate a compilation error, the
third (mc2 = Foo1();) compiles and runs without any problem. I am
wondering why the:

In neither case where you have an error is the instance of
MyClass a const, and you have said that the compiler is not
allowed to implicitely cast from non-const to const when
using the copy-ctor.
 
J

Jean Stax

Unfortunatelly, I can't see how your point makes the difference:

when I change my code to (the explicit attribute was removed):

MyClass(const MyClass& copy)
{
//...
}

my code get compilled succesfully.

However, when I change my code to (const was removed):

explicit MyClass(MyClass& copy)
{
//...
}

I still can't compile the first two cases.

Thanks.
 
T

Thomas Wintschel

Jean Stax said:
Hi !

I tried to understand when the explicit attribute in copy constructor
prevents from
me to create a new object. Bellow is the sample code.
While the two first cases really generate a compilation error, the
third (mc2 = Foo1();) compiles and runs without any problem. I am
wondering why the:

MyClass Foo1()
{
MyClass mc(1);
return mc;
}

isn't forbidden, when MyClass copy constructor is defined as explicit.

Thanks.


class MyClass
{
public:
MyClass(int nVal) : m_nVal1(nVal){}
MyClass() : m_nVal1(0){}
explicit MyClass(const MyClass& copy)
{
//...
}
private:
int m_nVal1;
};

void Foo(MyClass cc)
{
}

MyClass Foo1()
{
MyClass mc(1);
return mc;
}

int main(int argc, char* argv[])
{
MyClass mc(1);
MyClass mc1 = mc;
//error C2440: 'initializing' : cannot convert from 'class MyClass' to
//class MyClass' No copy constructor available for class 'MyClass'

Foo(mc);
//error C2664: 'Foo' : cannot convert parameter 1 from 'class MyClass'
//to 'class MyClass'No copy constructor available for class 'MyClass'

MyClass mc2;
mc2 = Foo1();


return 0;
}

Looking at my (now ancient) 2nd edition of Stroustrup, the first two require
implicit conversions and should not compile in any case. As for the third,
I will step out on a limb and say:

The first two both require the compiler to duplicate an existing object.
Foo1(), on the other hand, produces a temporary object. In this case, the
compiler could allow mc2 to be the actual object created by Foo1() (even
though two lines of code are involved) in which case it would not be
necessary to copy anything.

Tom
 
L

lilburne

Jean said:
Unfortunatelly, I can't see how your point makes the difference:

You are probably right.

GCC rejects three usages the return from Foo1(), and the two
instances you outlined.

The nearest I can ascertain is that the explicit specifier
on a copy ctor makes the ctor unusable in contexts where
'copy initialization' is performed, e.g., return by value
and pass by value. in addition to the MyClass mc1 = mc case.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top