473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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;
}
Jul 22 '05 #1
4 5833
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dario | last post by:
Trying to compile the following code-fragment with g++ 2.96: class Entity { private: void * data; public: explicit Entity(int); explicit Entity(Entity &); virtual ~Entity(); void...
22
by: Shea Martin | last post by:
I have a String class (I know I am re-inventing the wheel, yes I have heard of boost, and of QString). My copy constructor does a deep (strcpy) of the char *_buffer member. I have a member...
6
by: Christoph Bartoschek | last post by:
Hi, gcc 3.4 rejects the following program: class T { public: T() : a(3) {} explicit T(T const & other) : a(other.a) {} private: int a;
8
by: trying_to_learn | last post by:
Why do we need to explicitly call the copy constructor and the operator = , for base class and member objects in composition? ....book says "You must explicitly call the GameBoard copy-constructor...
2
by: Dave | last post by:
Hello NG, Can anybody fathom the purpose of an explicit copy constructor? On page 232 of the Josuttis STL reference, I see a reference to such. How could you ever need to supress the...
12
by: Mark E. Fenner | last post by:
Hello all, I have a code where my inner loop looks like: allNew = for params in cases: newObj = copy(initialObject) newObj.modify(params) allNew.append(newObj) return allNew
1
by: petschy | last post by:
hello, i've run into an error when qualifying a copy ctor 'explicit'. the strange thing is that i get a compiler error only if the class is a template and declare the variable as X<Zx = y....
1
by: =?gb2312?B?wfXquw==?= | last post by:
Hi, folks, I am trying to make my copy constructor explicit, but when the scenario below comes into being, weird things happen. Here is the code snippet: #include <iostream> using...
9
by: puzzlecracker | last post by:
From my understanding, if you declare any sort of constructors, (excluding copy ctor), the default will not be included by default. Is this correct? class Foo{ public: Foo(int); // no...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.