non-const copy ctor

Christian Fröbel cfroebel@web.de
Sat Apr 8 13:22:00 GMT 2006


Hi John,

John Love-Jensen wrote:
> [...]
> Regardless, it's not a copy constructor.

Okay, then let's not call it a copy constructor; I'm not fixed to that. How 
should I call it then? It is a special kind of constructor deserving a 
special name.

> [...]
> You can implement similar facilities like auto_ptr for your class to have
> the behavior you are inquiring about.

I guess so. Yet I still don't know how to do this.

> [...]
> On this particular issue, g++ is complying with ISO 14882.  VC7 is not.
>
> More information in The Design and Evolution of C++:
> http://www.amazon.com/gp/product/0201543303/

I don't have this book yet. But after a first glance I think it's worth 
buying.

> I don't recall if the particular point you raised is covered.
>
> I believe it (copy constructor being a const reference) has to do with
> catastrophic slicing if it were not a const, and infinite recursion if it
> were not a reference.

@2nd point: agreed
@1st point: not sure (don't even know what exactly catastrophic slicing 
means). From my restricted technical understanding passing a const reference 
it not different from passing a reference. The only difference is that the 
compiler doesn't let you make any non-const operations on a const object. 
It's like private and public methods. Technically there's no difference 
between them. Again, it's just that the compiler only let's you call a 
private method if you're allowed to.

That said, why don't I just do this:
A::A (const A& copyMe)
{
	A&   nonConstSource = const_cast<A&> (copyMe);
	// ...
}

Not nice of course, but does the trick.

Yours,
  Christian



More information about the Gcc-help mailing list