Copy Constructor and explicit attribute



 DEVELOP > c-Plus-Plus > Copy Constructor and explicit attribute

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Jean Stax"
Date: 22 Nov 2003 03:51:01 PM
Object: Copy Constructor and explicit attribute
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;
}
.

User: "Thomas Wintschel"

Title: Re: Copy Constructor and explicit attribute 23 Nov 2003 02:48:16 AM
"Jean Stax" <jean_stax@hotmail.com> wrote in message
news:df5c1ee6.0311221351.469cabb4@posting.google.com...

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
.

User: "lilburne"

Title: Re: Copy Constructor and explicit attribute 22 Nov 2003 04:36:39 PM
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.
.
User: "Jean Stax"

Title: Re: Copy Constructor and explicit attribute 23 Nov 2003 02:16:40 AM
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 <lilburne@godzilla.net> wrote in message news:<bpoof7$1qktci$1@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.

.
User: "lilburne"

Title: Re: Copy Constructor and explicit attribute 23 Nov 2003 11:12:53 AM
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.
.




  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER