views:

626

answers:

3

I have a not-so-small class under development (that it changes often) and I need not to provide a public copy constructor and copy assignment. The class has objects with value semantics, so default copy and assignment work.

the class is in a hierarchy, with virtual methods, so I provide a virtual Clone() to avoid slicing and to perform "polymorphic copy".

I don't want to declare copy assignment and construction protected AND to define them (and to maintain in-sync with changes) unless I have some special thing to perform.

Do someone knows if there's another way?

thanks!

UgaSofT

A: 

I don't think there is anything in the C++ language that allows you to do this. Although I'd love to be wrong on this point.

I've run into this in the past and come up with the following solution. Asumme the class is C1.

  1. Define a private inner class called Data
  2. Put all of my members I would delare in C1 on Data instead
  3. Define a protected copy constructor that just copies Data instances between C1.

This approach has a couple of downsides. Namely it feels a bit un-natural and eliminates direct field access (can be mitigated with small accessor functions).

It's a roundabout way of doing what you're looking for but it avoids you having to write the copy constructor by hand.

JaredPar
+2  A: 

An object from a polymorphic hierarchy, and with value semantics ? Something is wrong here.

If you really do need your class to have a value semantics, have a look at J.Coplien's Envelop-Letter Idiom, or at this article about Regular Objects [1].

[1] Sean Parent. “Beyond Objects”. Understanding The Software We Write. http://stlab.adobe.com/wiki/index.php/Papers_and_Presentations. C++ Connections. Nov 2005.

HTH,

Luc Hermitte
yes, it's possible. In my hierarchy I use Smart pointers to base classes of another hierarchy... The actual types of the data pointed by my smart pointers will be decided by derived classes, and deep copy is performed by virtual cloning. Default copy ctor is ok, so I have a value class.
ugasoft
Don't you have a slicing issue to solve ?Do you really need your objects to be copiable -- I've learned with time that often this is a false requirement: something we believe to be needed, but that actually we never use in the end.
Luc Hermitte
Maybe you are right... But just because I use my object itself via smart pointers... But I will sleep without nightmare if I'm sure none can copy my objects!
ugasoft
If you use your objects via ref-counter pointers, then they have a reference semantics, and do not need to be copied.
Luc Hermitte
yes but who ensures me that some code-criminal will not perform something like *sp1 = *sp2 ??
ugasoft
Just forbid it => inherit from a non copyable class (see boost::noncopyable), or make your assignment operator private and don't define it.
Luc Hermitte
ok, you convinced me! I accept your answer.
ugasoft
A: 

Maybe I've found a solution...

I can put in my root base class (or I can create a small interface class, with no data members, and perform multiple inheritance) the protected copy constructor that here is empty. I don't redefine the Copy ctor in derived classes, when the default one it's ok. Now the default copy ctor is not accessible to clients (cause the base is not accessible) but default works!

There are some objection?

ugasoft