簡體   English   中英

指向類成員函數的指針

[英]Pointer to class member function

我有兩個類,消息傳遞和GameObject。 每個GameObject對象都有一個Messaging實例,我需要在Messaging中有一個指向GameObject中函數的函數指針。

此外,還將有一些來自GameObject的類的對象具有相同的對象(在其Messaging實例中具有指向它們中的函數的函數指針)。

以下是GameCharacter對象(派生自GameObject)向其Messaging實例提供要調用的函數的代碼:

messaging->Register(GameCharacter::DoMessage);

這是我的消息傳遞類代碼:

/// Allows an object to send and receive messages.
class Messaging : public Component, public ::Messaging
{
private:

    // Forward declaration of GameObject
    class GameObject {};

    // Define the OnMessageType as a function pointer to a function
    // taking Message as parameter and returning void.
    typedef void (GameObject:: *OnMessageType)(Message);

    // OnMessage function pointer;
    OnMessageType OnMessage;

public:

    // Assign a function to the function pointer
    void Register(OnMessageType func) { OnMessage = func; }

    // Forward the Message to the function pointed to by the function pointer
    void DoMessage(Message msg) { (this->*OnMessage)(msg); };

};

GameObject類:

class GameObject
{
    public:

        /// Unique number used for identification
        int id;

        /// Reference to the graphical model displayed by this object
        int modelReference;

        /// GameObject children
        std::vector<GameObject> children;

        /// Constructor
        GameObject();

        /// Destructor
        ~GameObject();

        /// Add a child GameObject node
        void AddChild(GameObject child);

        /// Update Object
        virtual void Update();

        /// Handles received messages
        //virtual void DoMessage(Message msg);

        // Provide pointers to all possible components
        Components::Transform* transform;
        Components::RigidBody* rigidBody;
        Components::Messaging* messaging;

        // Pointer to message queue
        //std::vector<Message> *msgQueue;

    private:
        /// Send message
        void SendMessage(Message msg);
};

GameObject對象將使用OnMessage應該指向的函數調用Register。 然后,當調用DoMessage時,應調用GameObject中指向的函數。 因此,在這種情況下,調用DoMessage(在GameCharacter的Messaging實例中)應依次在GameCharacter對象中調用DoMessage。

問題是我在DoMessage(Message msg)函數上收到此錯誤:

指向成員類型'void(Components :: Messaging :: GameObject ::)(Message)'的指針與對象類型'Components :: Messaging'不兼容

從中我可以得出它對指針函數類型的不滿意。 我已經讀過(在堆棧溢出文章中),當調用指針函數時,應該指定成員函數所屬的類型,但是我找不到如何做的...

誰能對此有所啟發,並解釋一下如何解決我的代碼?

使用帶有std::bind std::function而不是函數指針。 收到此類錯誤的原因是,您沒有對象來接收函數調用,而只有一個函數。 如果需要通過其指針調用成員函數,則必須保留兩件事:

  1. 函數指針
  2. 保留了成員函數指針的類的對象

實現所需內容的最佳方法是將std::functionstd::bind 例:

using namespace std::placeholders;
GameCharacter gameCharacter;
messaging->Register(std::bind(GameCharacter::DoMessage, gameCharacter, _1));

並且Messaging類必須接受std::function<void(Message)>對象類型。

class Messaging : public Component, public ::Messaging
{
private:

    // Forward declaration of GameObject
    class GameObject {};

    // Define the OnMessageType as a function pointer to a function
    // taking Message as parameter and returning void.
    //typedef void (GameObject:: *OnMessageType)(Message);
    typedef std::function<void(Message)> OnMessageType;

    // OnMessage function pointer;
    OnMessageType OnMessage;

public:

    // Assign a function to the function pointer
    void Register(OnMessageType func) { OnMessage = func; }

    // Forward the Message to the function pointed to by the function pointer
    void DoMessage(Message msg) { this->OnMessage(msg); };

};

Messaging不會從GameObject繼承,因此您不能使用this實例來調用OnMessage

您需要一個GameObject實例來調用成員方法。

 GameObject gameObject;

 (gameObject.*OnMessage)(msg);
 // or (gameObject.*this->OnMessage)(msg);

但是不確定這是您想要做什么。

也許您真的想要std::function<void(Message )> ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM