By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

Copy Constructor and explicit attribute

P: n/a
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;
}
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Jean Stax wrote:
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.

Jul 22 '05 #2

P: n/a
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.

lilburne <li******@godzilla.net> wrote in message news:<bp*************@ID-203936.news.uni-berlin.de>...
Jean Stax wrote:
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.

Jul 22 '05 #3

P: n/a
"Jean Stax" <je*******@hotmail.com> wrote in message
news:df**************************@posting.google.c om...
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
Jul 22 '05 #4

P: n/a
Jean Stax wrote:
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.

Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.