473,848 Members | 1,788 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5875
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******@godzi lla.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*******@hotm ail.com> wrote in message
news:df******** *************** ***@posting.goo gle.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
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
15342
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 makeSomething();
22
2872
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 function func(const String &param). When an actual String is passed as the param this is nice and efficient. I have a constructor which takes a const char* as an argument. And
6
7527
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
2989
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 or the default constructor is automatically called instead" Why cant the compiler do this on its own. if we are making an object through copr construction for an inherited class , then why not simply call the corresponding copy constructors for...
2
2149
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 possibility of an implicit conversion from type T to type T? Such an implicit conversion could never occur because you're already of the required type! Thanks,
12
3108
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
2330
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. X<Zx(y) is fine. Tested with gcc 2.95, 3.3, 4.1, all gave the same error: t.cpp: In function 'int main()': t.cpp:44: error: no matching function for call to 'D<int>::D(D<int>&)'
1
2477
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 namespace std;
9
2907
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 Foo() is included, i believe. };
0
9738
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11001
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10664
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10348
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7894
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7069
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5919
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4544
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4137
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.