Copying a class that inherits from a class with pure virtual methods?

One way to do it would be to have add this to your Command class: public: virtual Command * Clone() const = 0 and then in the various subclasses of Command, implement Clone() to return a copy of the object: public: virtual Command * Clone() const {return new MyCommandSubclass(*this);} Once that's done, you can then do what you want like this: Command * copiedCommand = commandImplementation->Clone().

One way to do it would be to have add this to your Command class: public: virtual Command * Clone() const = 0; ... and then in the various subclasses of Command, implement Clone() to return a copy of the object: public: virtual Command * Clone() const {return new MyCommandSubclass(*this);} Once that's done, you can then do what you want like this: Command * copiedCommand = commandImplementation->Clone().

I bet Go makes this more elegant. – Stefan Kendall Nov 14 '09 at 18:39 YEs this would be a good answer for Java. – Loki Astari Nov 14 '09 at 18:52.

If Command is an abstract class, you won't be able to call the copy constructor directly. Instead, you might consider creating a clone() method that returns a deep copy of the object. This could also be a pure-virtual method that returns your base class, such as: class Command { public: virtual Command* clone(void) const = 0; }.

en.wikipedia.org/wiki/Prototype_pattern A prototype pattern is a creational design pattern used in software development when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. This pattern is used to: avoid subclasses of an object creator in the client application, like the abstract factory pattern does. Avoid the inherent cost of creating a new object in the standard way (e.g. , using the 'new' keyword) when it is prohibitively expensive for a given application.

To implement the pattern, declare an abstract base class that specifies a pure virtual clone() method. Any class that needs a "polymorphic constructor" capability derives itself from the abstract base class, and implements the clone() operation. The client, instead of writing code that invokes the "new" operator on a hard-coded class name, calls the clone() method on the prototype, calls a factory method with a parameter designating the particular concrete derived class desired, or invokes the clone() method through some mechanism provided by another design pattern.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions