Problem with Command Pattern under Visual Studio 2008 (C++) -
I ++ under this pattern VS 2008, the same code has been tested in GCC with a problem (MinGW for Linux, Mac and widnows) and it works
I copy / paste the code here:
< Pre> class MyCommand {public: execution on virtual zero () = 0; Virtual ~ myCommand () {}; }; Class MyOperation {Public: Virtual Zero DoIt () {}; // I do not even write it inline}; Class MyOperationDerived: Public MyOperation {Public: Virtual Zero DoIt () {}; // I do not even write it inline}; Class MyUndoStackCommand: Public MyCommand {Public: typedef zero (MyOperation :: * action); MyUndostac Commands (My Operation * REC, Action Action); / * Virtual * / zero executed (); / * Virtual * / ~ myundostaccommand (); Private: My Operation * My Receiver; Action mynection; };
The CPP:
{#include "MyUndoStackCommand.h" #include "MyOperation.h" MyUndoStackCommand :: ~ MyUndoStackCommand ()} MyUndoStackCommand :: MyUndoStackCommand (myOperation * Rec action): myReceiver (REC), myAction (action) {} void MyUndoStackCommand :: execute () {((myReceiver) - & gt; * (myAction) ()); }
Use in main.cpp:
MyReceiver receiver; MyDindostak Commands USC (and Receiver, and Myoporation :: IOT); Usc.execute ();
When I debug under Visual Studio only if I set inside MyUndoStackCommand
, directly myAction = & amp; MyOperation :: Chhand
, it works
Any advice?
Thank you very much, Dan
Edit: The following code is compiled with g ++ - changes made by Neil Butterworth in the form of // NB Has been flagged in.
class MyCommand {public: virtual void execute () = 0; Virtual ~ myCommand () {}; }; Class MyOperation {Public: Virtual Zero DoIt () {}; // I do not even write it inline}; Class MyOperationDerived: Public MyOperation {Public: Virtual Zero DoIt () {}; // I do not even write it inline}; Class MyUndoStackCommand: Public MyCommand {Public: typedef zero (MyOperation :: * action); MyUndostac Commands (My Operation * REC, Action Action); / * Virtual * / zero executed (); / * Virtual * / ~ myundostaccommand (); Private: My Operation * My Receiver; Action mynection; }; MyUndoStackCommand :: ~ MyUndoStackCommand () {} MyUndoStackCommand :: MyUndoStackCommand (MyOperation * rec; // nb Action): myReceiver (REC), myAction (action) {} void MyUndoStackCommand :: Execute () {((myReceiver) - & Gt * (myAction)) (); } Int main () {My Operation Receiver; // NB MyUndoStackCommand usc (and the receiver, and myopathy :: DoIt); Usc.execute (); }
This work is fine in Visual Studio 2008 with Nileks.
// command.h #pragma once #include & lt; Iostream & gt; // Namespace was added for printing using std; // Added MyCommand for Printing Class {Public: Virtual Zero Execute () = 0; Virtual ~ myCommand () {}; }; Class My Operation {Public: Virtual Zero (ITIT) {cout & lt; & Lt; "Myoperation :: doit ()" & lt; & Lt; Endl; }; // I do not even write it inline}; Class MyOperationDerived: Public MyOperation {Public: Virtual Zero DoIt () {cout & lt; & Lt; "Myoperderived :: doit ()" & lt; & Lt; Endl; }; // I do not even write it inline}; Class MyUndoStackCommand: Public MyCommand {Public: typedef zero (MyOperation :: * action); MyUndostac Commands (My Operation * REC, Action Action); / * Virtual * / zero executed (); / * Virtual * / ~ myundostaccommand (); Private: My Operation * My Receiver; Action mynection; }; // command.cpp #include "command.h" MyUndoStackCommand :: ~ MyUndoStackCommand () {} MyUndoStackCommand :: MyUndoStackCommand :: MyUndoStackCommand (/ * M * / Meraopreshn * Rec Action): myReceiver (rec), myAction (verb } {} Zero MyUndoStackCommand :: execute () {((myReceiver) -> * (myAction)) (); } // main.cpp #include "command.h" int main () {MyOperationDerived Receiver; MyDindostak Commands USC (and Receiver, and Myoporation :: IOT); Usc.execute (); }
will print:
"myoperderwise :: doit ()"
Comments
Post a Comment