Visual Studio 2008下的命令模式问题(C ++)

时间:2010-06-15 08:16:32

标签: c++ visual-studio-2008 pointers

我在VS 2008上的c ++下遇到了这种模式的问题。 相同的代码已经在gcc中测试过(linux,mac和mingw for widnows)并且它有效。

我在这里复制/粘贴代码:

class MyCommand {

public:
    virtual void execute() = 0;
    virtual ~MyCommand () {};
};

class MyOperation {
public:
    virtual void DoIt() {}; //I also write it not inline
};

class MyOperationDerived : public MyOperation {
public:
    virtual void DoIt() {}; //I also write it not inline
};


class MyUndoStackCommand : public MyCommand {
 public:
     typedef void(MyOperation::*Action)();
     MyUndoStackCommand(MyOperation *rec, Action action);
     /*virtual*/ void execute();
     /*virtual*/ ~MyUndoStackCommand();

private:
    MyOperation *myReceiver;
    Action myAction ;
};

在cpp:

#include "MyUndoStackCommand.h"
#include "MyOperation.h"

MyUndoStackCommand::~MyUndoStackCommand() {
}

MyUndoStackCommand::MyUndoStackCommand(myOperation *rec, Action
action): myReceiver(rec), myAction(action) {
}

void MyUndoStackCommand::execute() {
   ((myReceiver)->*(myAction))();
}

在main.cpp中使用:

MyReceiver receiver;
MyUndoStackCommand usc(&receiver, &MyOperation::DoIt);
usc.execute();

当我在visual studio下调试时,只有直接在MyUndoStackCommand内设置 myAction = &MyOperation::DoIt,它有效,否则无效。

有什么建议吗?

非常感谢你, 担

编辑:以下代码使用g ++编译 - Neil Butterworth的更改标记为// NB。

class MyCommand {

public:
    virtual void execute() = 0;
    virtual ~MyCommand () {};
};

class MyOperation {
public:
    virtual void DoIt() {}; //I also write it not inline
};

class MyOperationDerived : public MyOperation {
public:
    virtual void DoIt() {}; //I also write it not inline
};


class MyUndoStackCommand : public MyCommand {
 public:
     typedef void(MyOperation::*Action)();
     MyUndoStackCommand(MyOperation *rec, Action action);
     /*virtual*/ void execute();
     /*virtual*/ ~MyUndoStackCommand();

private:
    MyOperation *myReceiver;
    Action myAction ;
};

MyUndoStackCommand::~MyUndoStackCommand() {
}

MyUndoStackCommand::MyUndoStackCommand(MyOperation *rec,  //NB
                                Action action)
    : myReceiver(rec), myAction(action) {
}

void MyUndoStackCommand::execute() {
   ((myReceiver)->*(myAction))();
}

int main() {
    MyOperation receiver;    //NB
    MyUndoStackCommand usc(&receiver, &MyOperation::DoIt);
    usc.execute();
}

1 个答案:

答案 0 :(得分:0)

使用Neils编辑,这在Visual Studio 2008中可以正常工作。

// command.h
#pragma once
#include <iostream>      // added for printing
using namespace std;     // added for printing
class MyCommand {
public:
    virtual void execute() = 0;
    virtual ~MyCommand () {};
};
class MyOperation {
public:
    virtual void DoIt() {
        cout << "myoperation::doit()" << endl;
    }; //I also write it not inline
};
class MyOperationDerived : public MyOperation {
public:
    virtual void DoIt() {
        cout << "myoperationderived::doit()" << endl;
    }; //I also write it not inline
};
class MyUndoStackCommand : public MyCommand {
 public:
     typedef void(MyOperation::*Action)();
     MyUndoStackCommand(MyOperation *rec, Action action);
     /*virtual*/ void execute();
     /*virtual*/ ~MyUndoStackCommand();
private:
    MyOperation *myReceiver;
    Action myAction;
};
// command.cpp
#include "command.h"
MyUndoStackCommand::~MyUndoStackCommand() {
}
MyUndoStackCommand::MyUndoStackCommand(/*m*/ MyOperation *rec, Action
action): myReceiver(rec), myAction(action) {
}
void MyUndoStackCommand::execute() {
   ((myReceiver)->*(myAction))();
}
// main.cpp
#include "command.h"
int main(){ 
 MyOperationDerived receiver;
 MyUndoStackCommand usc(&receiver, &MyOperation::DoIt);
 usc.execute();
}

将打印:

"myoperationderived::doit()"