inheritance - C++ How to use stand-alone base object from derived object -
I have 3 classes that have a basis, a parent and a child. Parents, at some point, instant and assigned data, in some cases it is used directly, but others require the functionality to be implemented in their child class.
What I would like to do, to put the original object in a child object, its virtual function will be used with the parent data:
Undo: Public Quando Commands {Public: UndoBase () {} Virtual Zero Undo () = 0; Virtual zero redo () = 0; }; Class CommandPaint: Public Undocumented {Public: CommandPracture ()}} Virtual Zero Undo () Virtual Wardo Redo () Protected: Some Type Data}; Class CommandChild: Public CommandPaint {Public: Command Chald () {} Virtual Zero Undo () // Override Parent Virtual Zero Redo () // Override Parent};
I have tried to do this:
Command practice * Parents = new command practice; // Assign data to * command = stabilized & lt; Command chain * & gt; (Guardian); Command & gt; to redo ();
And it worked (command child implementation and commandpatter data used) However, I swear it should be undefined behavior (whatever works, I know) . Since I suspect that this is the right thing to do, I want to ask if it is okay or if there are other ways beside the structure.
undo * order = static_caast < Command chain * & gt; (Guardian);
It is in every hell of every inch of this line, you are telling static_cast to put the command child in the command chain.
In addition to this, you are trying to insert Command Child with the command child, which means that the base class has been generated where the base class has no knowledge of the cast cast derived. Even if you write it properly
Command child * child = static_cast & lt; Command chain * & gt; (Guardian);
This would be wrong. Apart from this, always use dynamic_cast for these cases. It clearly checks whether the artist is possible or not. If the cast is safe or cast, in the case of signs (for reference, this bad_cast throws an exception) if it can not target the target type.
Comments
Post a Comment