[英]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.