English 中文(简体)
Copying a class that inherits from a class with pure virtual methods?
原标题:

I ve not used C++ in a while, and I ve become far too comfortable with the ease-of-use of real languages.

At any rate, I m attempting to implement the Command pattern, and I need to map a number of command object implementations to string keys. I have an STL map of string to Command, and I d like to copy the Command.

Essentially,

Command * copiedCommand = new Command( commandImplementation );

And I d like to retain the functionality of commandImplementation. Since Command has the pure virtual function execute, this doesn t work. What s the correct way to do this?

最佳回答

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();
问题回答

http://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.

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;
};




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签