簡體   English   中英

成員函數指向成員函數的指針

[英]member function pointer to member function

關於成員函數的指針有很多討論,但是我很難理解成員函數的隱藏const狀態的問題。 誰能給我一個簡單的答案,我在這里做錯了什么?

class Entity
{
public:
    Entity();
    void(Entity::*update_function)();
private:
    void update_mode_1() {
    }
};

Entity::Entity()
{
    update_function = update_mode_1;
    //error C3867: 'Entity::update_mode1': non-standard syntax; use '&' to create a pointer to member
}

void test_init() {
    Entity obj;
    obj.update_function();
    //Error: Expression preceding parenthesis of apparent call must have (pointer-to-) function type
}

這是解決錯誤的方法,無論如何,這似乎與“成員函數的隱藏const狀態”無關。

錯誤C3867:'Entity :: update_mode1':非標准語法; 使用“&”創建指向成員的指針

如錯誤消息所述,使用&創建指向成員的指針。

update_function = &Entity::update_mode_1;

錯誤:顯式調用括號前的表達式必須具有(指針指向)函數類型

使用指針成員訪問運算符 (例如.* )進行調用。

(obj.*(obj.update_function))();

生活

暫無
暫無

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

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