[英]Using Wrapper with function pointers to handle default value for arguments
我有一個用於C接口的C ++包裝器類。 該接口中的一個函數具有一個帶默認參數的參數:
api.h:
int Foo(int bar=5);
這是包裝器:
Wrapper.hpp:
class Wrapper
{
public:
static int (*Foo) (int bar);
}
Wrapper.cpp:
int (*Wrapper::Foo)(int bar);
這是我將函數與包裝器一起使用的地方:
Wrapper::Foo(5);
但我也希望能夠不帶參數地調用Foo
,因此它采用默認值5
:
Wrapper::Foo();
我怎樣才能做到這一點?
僅使用函數指針是無法做到這一點的,因為函數指針禁止使用默認參數,並且不能將函數int(int)
分配給函數int()
。
N3376 8.3.6 / 3
默認參數只能在函數聲明的parameter-declaration-clause中指定
為什么要使用指針? 只需編寫函數,即可從API調用函數。
class Wrapper
{
public:
static int Foo (int bar)
{
return ::Foo(bar);
}
static int Foo ()
{
return ::Foo();
}
}
Wrapper::Foo(1);
Wrapper::Foo();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.