当我们通过引用传递参数时,是否可以为函数的参数提供默认值。在 C++ 中
例如,当我尝试声明如下函数时:
virtual const ULONG Write(ULONG &State = 0, bool sequence = true);
当我这样做时,它给出了一个错误:
错误 C2440:“默认参数”:无法从“const int”转换为“unsigned long &”非“const”的引用不能绑定到非左值
当我们通过引用传递参数时,是否可以为函数的参数提供默认值。在 C++ 中
例如,当我尝试声明如下函数时:
virtual const ULONG Write(ULONG &State = 0, bool sequence = true);
当我这样做时,它给出了一个错误:
错误 C2440:“默认参数”:无法从“const int”转换为“unsigned long &”非“const”的引用不能绑定到非左值
您可以为 const 引用执行此操作,但不能为非常量引用执行此操作。这是因为 C++ 不允许将临时(在这种情况下为默认值)绑定到非常量引用。
解决此问题的一种方法是使用实际实例作为默认实例:
static int AVAL = 1;
void f( int & x = AVAL ) {
// stuff
}
int main() {
f(); // equivalent to f(AVAL);
}
但这是非常有限的实际用途。
已经在对您的答案的直接评论之一中说过,但只是正式声明。您要使用的是重载:
virtual const ULONG Write(ULONG &State, bool sequence);
inline const ULONG Write()
{
ULONG state;
bool sequence = true;
Write (state, sequence);
}
使用函数重载还有其他好处。首先,您可以默认任何您希望的参数:
class A {};
class B {};
class C {};
void foo (A const &, B const &, C const &);
void foo (B const &, C const &); // A defaulted
void foo (A const &, C const &); // B defaulted
void foo (C const &); // A & B defaulted etc...
也可以在派生类中重新定义虚函数的默认参数,这可以避免重载:
class Base {
public:
virtual void f1 (int i = 0); // default '0'
virtual void f2 (int);
inline void f2 () {
f2(0); // equivalent to default of '0'
}
};
class Derived : public Base{
public:
virtual void f1 (int i = 10); // default '10'
using Base::f2;
virtual void f2 (int);
};
void bar ()
{
Derived d;
Base & b (d);
d.f1 (); // '10' used
b.f1 (); // '0' used
d.f2 (); // f1(int) called with '0'
b.f2 (); // f1(int) called with '0
}
只有一种情况真正需要使用默认值,那就是在构造函数上。不可能从另一个构造函数调用一个构造函数,因此这种技术在这种情况下不起作用。
仍然有提供可选参数的旧 C 方法:当不存在时可以为 NULL 的指针:
void write( int *optional = 0 ) {
if (optional) *optional = 5;
}
这个小模板将帮助您:
template<typename T> class ByRef {
public:
ByRef() {
}
ByRef(const T value) : mValue(value) {
}
operator T&() const {
return((T&)mValue);
}
private:
T mValue;
};
然后,您将能够:
virtual const ULONG Write(ULONG &State = ByRef<ULONG>(0), bool sequence = true);
通过引用传递参数有两个原因:(1) 出于性能考虑(在这种情况下,您希望通过 const 引用传递)和 (2) 因为您需要能够更改函数内部参数的值。
我非常怀疑在现代架构上传递一个无符号的 long 会让你放慢速度。所以我假设您打算更改State
方法内部的值。编译器抱怨是因为常量0
无法更改,因为它是右值(错误消息中的“非左值”)且不可更改(const
错误消息中)。
简单地说,您想要一个可以更改传递的参数的方法,但默认情况下您希望传递一个无法更改的参数。
换句话说,非const
引用必须引用实际变量。函数签名 ( 0
) 中的默认值不是实变量。您遇到了与以下相同的问题:
struct Foo {
virtual ULONG Write(ULONG& State, bool sequence = true);
};
Foo f;
ULONG s = 5;
f.Write(s); // perfectly OK, because s is a real variable
f.Write(0); // compiler error, 0 is not a real variable
// if the value of 0 were changed in the function,
// I would have no way to refer to the new value
如果您实际上并不打算State
在方法内部进行更改,则可以简单地将其更改为const ULONG&
. 但是您不会从中获得很大的性能优势,因此我建议将其更改为 non-reference ULONG
。我注意到您已经返回 a ULONG
,并且我偷偷怀疑它的值是State
经过任何需要的修改后的值。在这种情况下,我将简单地声明该方法:
// returns value of State
virtual ULONG Write(ULONG State = 0, bool sequence = true);
当然,我不太确定你在写什么或写到哪里。但这是另一个问题。
不,这是不可能的。
通过引用传递意味着该函数可能会更改参数的值。如果参数不是由调用者提供并且来自默认常量,那么函数应该改变什么?
您不能将常量文字用作默认参数,原因与您不能将常量文字用作函数调用的参数相同。引用值必须有地址,常量引用值不需要(即它们可以是 r 值或常量字面量)。
int* foo (int& i )
{
return &i;
}
foo(0); // compiler error.
const int* bar ( const int& i )
{
return &i;
}
bar(0); // ok.
确保你的默认值有一个地址,你很好。
int null_object = 0;
int Write(int &state = null_object, bool sequence = true)
{
if( &state == &null_object )
{
// called with default paramter
return sequence? 1: rand();
}
else
{
// called with user parameter
state += sequence? 1: rand();
return state;
}
}
我已经使用过这种模式几次,我有一个参数可以是变量或空值。常规方法是让用户在这种情况下传递一个指针。如果他们不想让你填写值,他们会传入一个 NULL 指针。我喜欢空对象方法。它使调用者的生活更轻松,而不会使被调用者代码非常复杂。
在 OO 的情况下...要说给定类具有并且“默认值”意味着必须相应地声明此默认值(值),然后可以将其用作默认参数,例如:
class Pagination {
public:
int currentPage;
//...
Pagination() {
currentPage = 1;
//...
}
// your Default Pagination
static Pagination& Default() {
static Pagination pag;
return pag;
}
};
在你的方法...
shared_ptr<vector<Auditoria> >
findByFilter(Auditoria& audit, Pagination& pagination = Pagination::Default() ) {
这种解决方案非常合适,因为在这种情况下,“全局默认分页”是一个单一的“参考”值。您还可以在运行时更改默认值,例如“全局级别”配置,例如:用户分页导航首选项等。
另一种方法可能如下:
virtual const ULONG Write(ULONG &State, bool sequence = true);
// wrapper
const ULONG Write(bool sequence = true)
{
ULONG dummy;
return Write(dummy, sequence);
}
那么以下调用是可能的:
ULONG State;
object->Write(State, false); // sequence is false, "returns" State
object->Write(State); // assumes sequence = true, "returns" State
object->Write(false); // sequence is false, no "return"
object->Write(); // assumes sequence = true, no "return"
我认为不是,原因是默认值被评估为常量,并且通过引用传递的值必须能够更改,除非您也将其声明为常量引用。
使用状态的 const 限定符是可能的:
virtual const ULONG Write(const ULONG &State = 0, bool sequence = true);
void f(const double& v = *(double*) NULL)
{
if (&v == NULL)
cout << "default" << endl;
else
cout << "other " << v << endl;
}
void revealSelection(const ScrollAlignment& = ScrollAlignment::alignCenterIfNeeded, bool revealExtent = false);
我有一个解决方法,请参阅以下有关默认值的示例int&
:
class Helper
{
public:
int x;
operator int&() { return x; }
};
// How to use it:
void foo(int &x = Helper())
{
}
您可以为任何您想要的琐碎数据类型执行此操作,例如bool
,double
...
定义 2 个重载函数。
virtual const ULONG Write(ULONG &State, bool sequence = true);
virtual const ULONG Write(bool sequence = true)
{
int State = 0;
return Write(State, sequence);
}
这也有一个相当肮脏的技巧:
virtual const ULONG Write(ULONG &&State = 0, bool sequence = true);
在这种情况下,您必须使用以下命令调用它std::move
:
ULONG val = 0;
Write(std::move(val));
这只是一些有趣的解决方法,我完全不建议在实际代码中使用它!
virtual const ULONG Write(ULONG &State = 0, bool sequence = true);
答案很简单,我不太擅长解释,但是如果您想将默认值传递给可能会在此函数中修改的非常量参数,请像这样使用它:
virtual const ULONG Write(ULONG &State = *(ULONG*)0, bool sequence =
> true);