如何正确覆盖"no non-const reference to temporary object"



我有一个类FooFoo有一些非const方法。我可以在临时Foo对象上调用非const方法;我只对这些方法实际返回的内容感兴趣,而不关心它们对Foo对象本身做了什么。

第一个问题:这本身是否表明Foo类设计得不好?

第二个问题:如果我想继续使用Foo,但我仍然希望能够通过引用将调用非const方法的函数来传递Foo对象,那么最好的方法是什么?

这是我得出的结论:

// The function that performs work on a Foo object.
int TakeFoo (Foo& FooBar) { ... }
// An overload just to accept temporary Foo objects.
int TakeFoo (Foo&& FooBar)
{
    // Delegate
    return TakeFoo(FooBar);
}

另一种方法就是这样做:

int TakeFoo (const Foo& FooBar)
{
    Foo& MyFooBar = const_cast<Foo&>(FooBar);
    // Do work on MyFooBar
}

但是这种方法有一个问题,你可能会把一个实际上声明为const的对象上的const转换掉,这将使我处于未定义的行为领域。

编辑:

使用TakeFoo:

的代码示例
Foo GimmeFoo() { ... }
cout << TakeFoo(GimmeFoo()) << endl;
Foo ConstructedFoo(...);
cout << TakeFoo(ConstructedFoo) << endl;
// Continue to use ConstructedFoo

回答你的第二个问题:

如果你的函数TakeFoo打算调用Foo的非const成员,那么使用

int TakeFoo (Foo& FooBar);

如果你确定TakeFoo只接受右值作为参数,那么使用

int TakeFoo (Foo&& FooBar);

如果你想对Foo做一些改变来计算int的返回值,那么使用

int TakeFoo (const Foo& FooBar)
{
    Foo FooBar MyFooBar = FooBar;
    // do something with MyFooBar and return int
}

int TakeFoo (Foo FooBar);

回答你的第一个问题:

int TakeFoo (FooBar)不应该为了计算int的结果而改变FooBar。更好的设计应该是

Foo transform_foo(Foo const& foo);
int compute_result(Foo const& foo);
int TakeFoo(Foo const& FooBar)
{
    return compute_result( transform_foo(FooBar) );
}

相关内容

最新更新