简体   繁体   English

如何使用类成员函数作为默认值

[英]How to use a class member function as defualt value

I have this class我有这堂课

class InventoryManagement
{
public:
    typedef void(InventoryManagement::*Action)(void);
    typedef void(InventoryManagement::*ActionWithReturn)(InventoryItem);

    void Initialize(int inventoryQty, int maxQty, ActionWithReturn onAdd = nullptr , Action onMaxQtyReach = nullptr, Action onQtyReset = nullptr, Action onSideChnage = nullptr);

    Action getStrategyOnMaxQtyReach();
    Action getStrategyOnQtyReset();
    Action getStrategyOnSideChange();
    ActionWithReturn getOnAdd();

private:

    // Private Functions Data members
    ActionWithReturn m_onAdd;
    Action m_OnSideChange;
    Action m_OnQtyReset;
    Action m_OnMaxQtyReach;


    // Default Function 
    void OnAddDefault(InventoryItem item) { }
    void OnMaxQtyReachDefault() { }
    void OnQtyResetDefault() { }
    void OnSideChangeDefault() { }
};

The function Initialize sets the data members of type Action .函数Initialize设置Action类型的数据成员。 I created default actions that do noting and I want to pass them as default arguments.我创建了不注意的默认操作,我想将它们作为默认参数传递。 If the user doesn't send any function pointer to Initialize then those functions will be set.如果用户没有向Initialize发送任何函数指针,那么这些函数将被设置。

The signature of the function is:函数的签名是:

void Initialize(int inventoryQty, int maxQty, ActionWithReturn onAdd = nullptr , Action onMaxQtyReach = nullptr, Action onQtyReset = nullptr, Action onSideChnage = nullptr);

I tried void Initialize(int inventoryQty, int maxQty, ActionWithReturn onAdd = InventoryManagement::OnAddDefault , Action onMaxQtyReach = nullptr, Action onQtyReset = nullptr, Action onSideChnage = nullptr);我试过void Initialize(int inventoryQty, int maxQty, ActionWithReturn onAdd = InventoryManagement::OnAddDefault , Action onMaxQtyReach = nullptr, Action onQtyReset = nullptr, Action onSideChnage = nullptr); instead of nullptr , but the compiler emitted "Call to non-static member function without an object argument" .而不是nullptr ,但编译器发出"Call to non-static member function without an object argument"

The syntax is &InventoryManagement::OnAddDefault :语法是&InventoryManagement::OnAddDefault

void Initialize(
    int inventoryQty,
    int maxQty,
    ActionWithReturn onAdd = &InventoryManagement::OnAddDefault, // <-- 
    Action onMaxQtyReach = nullptr,
    //... etc
)

On a side node, I encourage you to consider using constructors instead of initialize methods.在侧节点上,我鼓励您考虑使用构造函数而不是初始化方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在类中使用成员函数专门化? - How to use member function specialization in class? 如何在类中的成员函数中更改值? - How to change a value in a member function within a class? 如何使用 class 的成员 function 作为另一个 ZA2F2ED4F8EBC2CBB4C21A29 的朋友 function - How to use a member function of a class as friend function of another class? 如何转发声明一个类的成员函数在另一个类中使用? - How to forward declare a member function of a class to use in another class? c ++ 11 Thread类如何使用类成员函数 - c++11 Thread class how to use a class member function 向上转换到基础 class 时如何使用继承的 class 成员的值 - How to use value of inherited class member, when upcasting to base class 如何使用boost :: bind绑定类成员函数? - How can I use boost::bind to bind a class member function? 如何将成员函数用于同一类的不同对象 - How to use a member function for diferent objects of same class 如何通过boost ::协程正确使用类成员函数? - How to properly use a class member function with boost::coroutine? 如何在不同的 switch case 语句中使用 object 的 class 成员 function? - How to use class member function of an object on different switch case statement?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM