[英]default value of const reference parameter
目前我有两个功能:
void foo(const A & a) {
...
result = ...
result += handle(a); // 1
bar(result);
}
void foo() {
...
result = ...
bar(result);
}
foo()中的所有代码都是相同的,除了1
。
我可以将它们合并到一个函数,如下所示吗?
void foo(const A & a = 0) {
...
...
if (a) result += handle(a); // this won't work, but can I do something similar?
bar(result);
}
顺便说一句,参数必须是一个参考,因为我想保持接口不变。
您可以使用Null对象模式 。
namespace
{
const A NULL_A; // (possibly "extern")
}
void foo(const A & a = NULL_A) {
...
result = ...
if (&a != &NULL_A) result += handle(a);
bar(result);
}
不可以。引用始终是真实对象的别名(假设您没有触发未定义的行为)。 您可以通过接受指针来实现类似的行为而无需代码重复:
void foo_impl(A const* obj) {
// code as before, except access obj with -> rather than .
}
void foo (A const& obj) {
foo_impl(&obj);
}
void foo() {
foo_impl(nullptr);
}
根据DRY的精神,为什么不将它们合并呢?
void foo(const A & a) {
foo();
handle(a);
}
void foo() {
...
...
}
使用引用的整个想法是避免NULL指针问题。 引用只是真实对象的别名。 我对你的程序有另一个简单的想法,基本上你想要使用相同的功能实现两个功能 - 使用默认参数。 这是代码。 请原谅我的变量名称。
class ABC
{
public:
int t;
ABC operator=(ABC& other)
{
other.t = 0;
}
};
ABC other;
void foo( ABC &a=other);
void foo( ABC &a)
{
if( a.t == 0)
qDebug()<<"The A WAS ZERO";
else
qDebug()<<"THE A isn't zero";
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
ABC hello;
hello.t = 100;
foo();
foo(hello);
return a.exec();
}
这是输出。
The A WAS ZERO
THE A isn't zero
使用提供接口的基类和实现接口的两个派生类,一个使用A
而另一个不使用任何东西。
重构foo
使用常见的foo
。
struct Handler
{
virtual int get() = 0;
};
struct AHandler : Handler
{
AHandler(const A& a) : a_(a) {}
virtual int get() { return handle(a_); }
const A& a_;
}
struct NullHandler : Handler
{
virtual int get() { return 0; }
}
void foo(const Handler & h) {
...
result = ...
result += h.get();
bar(result);
}
void foo(const A & a) {
AHandler ah(a);
foo(ah);
}
void foo() {
NullHandler nh(a);
foo(nh);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.