Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > multiple copy constructors

Reply
Thread Tools

multiple copy constructors

 
 
Glen Able
Guest
Posts: n/a
 
      02-19-2004
Any ideas why compilers seem to give warnings about multiple copy
constructors e.g.

class A
{
public:
A(A& a);
A(const A& a);
};


Also, are there any other forms which would constitute a copy ctor?

thanks.


 
Reply With Quote
 
 
 
 
David Rubin
Guest
Posts: n/a
 
      02-19-2004
Glen Able wrote:

> Any ideas why compilers seem to give warnings about multiple copy
> constructors e.g.
>
> class A
> {
> public:
> A(A& a);
> A(const A& a);
> };


The problem is that a two functions cannot be differentiated solely on the basis
of the 'constness' of their arguments.

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
 
Reply With Quote
 
 
 
 
Guillaume Brocker
Guest
Posts: n/a
 
      02-19-2004
David Rubin wrote:

> Glen Able wrote:
>
>> Any ideas why compilers seem to give warnings about multiple copy
>> constructors e.g.
>>
>> class A
>> {
>> public:
>> A(A& a);
>> A(const A& a);
>> };

>
> The problem is that a two functions cannot be differentiated solely on
> the basis of the 'constness' of their arguments.


That's right. Furthermore, IMHO, it's non sens to pass a non constant
parameter to a copy constructor, since the copy constructor's role is to
only make a *copy* of the passed parameter.

So there is absolutely no reason to have a constructor like this:

A(A& a)

--
Guillaume Brocker
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      02-19-2004
>
> That's right. Furthermore, IMHO, it's non sens to pass a non constant
> parameter to a copy constructor, since the copy constructor's role is to
> only make a *copy* of the passed parameter.
>
> So there is absolutely no reason to have a constructor like this:
>
> A(A& a)
>


std::auto_ptr has a copy constructor exactly like that.

It's not common, but it is legal and very occasionally useful.

john


 
Reply With Quote
 
Rob Williscroft
Guest
Posts: n/a
 
      02-20-2004
David Rubin wrote in news:c12p1g$(E-Mail Removed):

> Glen Able wrote:
>
>> Any ideas why compilers seem to give warnings about multiple copy
>> constructors e.g.
>>
>> class A
>> {
>> public:
>> A(A& a);
>> A(const A& a);
>> };

>
> The problem is that a two functions cannot be differentiated solely on
> the basis of the 'constness' of their arguments.


No, all the usual overload resolution rules apply:

#include <iostream>

struct X
{
X() { std::cerr << "X::X()\n";}
X( X const & ) { std::cerr << "X::X( X const & )\n"; }
X( X & ) { std::cerr << "X::X( X & )\n"; }
};


X f() { return X(); }


int main()
{
using namespace std;
X const x = f();
X y( x );
X z( y );
}

I get:
X::X()
X::X( X const & )
X::X( X & )

With all 5 compilers I tried, for some reason VC 7.1 gives
the warning descibed by the OP. I can't think of a good reason
for it, but VC 7.1 has a whole bunch of backward compatability
features that often don't make sense, so maybe thats it.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
David Rubin
Guest
Posts: n/a
 
      02-20-2004
Rob Williscroft wrote:

> David Rubin wrote in news:c12p1g$(E-Mail Removed):
>
>
>>Glen Able wrote:
>>
>>
>>>Any ideas why compilers seem to give warnings about multiple copy
>>>constructors e.g.
>>>
>>>class A
>>>{
>>>public:
>>> A(A& a);
>>> A(const A& a);
>>>};

>>
>>The problem is that a two functions cannot be differentiated solely on
>>the basis of the 'constness' of their arguments.

>
>
> No, all the usual overload resolution rules apply:


[snip]
> With all 5 compilers I tried, for some reason VC 7.1 gives
> the warning descibed by the OP. I can't think of a good reason
> for it, but VC 7.1 has a whole bunch of backward compatability
> features that often don't make sense, so maybe thats it.


You're right. After I posted my comment, I tried an example only to find I was
incorrect. Thanks,

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reg constructors/copy constructors inheritance srp113 C++ 3 02-17-2009 04:01 PM
Is the possible to have all the public constructors of the publicbase class as the constructors of a derived class? Peng Yu C++ 5 09-19-2008 10:19 AM
compiler synthesized constructors/copy constructors/assignment operators Jess C++ 5 06-07-2007 11:09 AM
Copy constructors, de/constructors and reference counts Jeremy Smith C++ 2 08-02-2006 11:25 PM
Constructors that call other Constructors Dave Rudolf C++ 12 02-06-2004 03:26 PM



Advertisments