如何覆盖“对于临时对象没有非常量引用”正确 [英] How to override "no non-const reference to temporary object" correctly

查看:192
本文介绍了如何覆盖“对于临时对象没有非常量引用”正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类 Foo Foo 有一些非const方法。我可以在一个临时的 Foo 对象上调用非const方法;我只感兴趣的方法实际返回,比他们对 Foo 对象本身做的事情。



第一个问题:这本身是否必须表明类 Foo / p>

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



这是我到达的:

  //对Foo对象执行工作的函数。 
int TakeFoo(Foo& FooBar){...}

//只接受临时Foo对象的重载。
int TakeFoo(Foo& FooBar)
{
//委托
return TakeFoo(FooBar);
}

另一种方法只是这样做:

  int TakeFoo(const Foo& FooBar)
{
Foo& MyFooBar = const_cast< Foo&>(FooBar);
//在MyFooBar上工作
}

但是这种方法有问题你可以在一个实际上被声明为const的对象上const成功去掉const,这会把我放在undefined-behavior-land中。



编辑: / p>

使用TakeFoo的代码示例:

  Foo GimmeFoo ..} 

cout<< TakeFoo(GimmeFoo())<< endl;

Foo ConstructedFoo(...);

cout<< TakeFoo(ConstructedFoo)<< endl;

//继续使用ConstructedFoo


解决方案

回答您的第二个问题:



如果您的函数TakeFoo意图调用Foo的非const成员,请使用

  int TakeFoo(Foo& FooBar); 

如果您确定TakeFoo只接受一个右值作为参数,请使用

  int TakeFoo(Foo& FooBar); 

如果要对Foo进行一些更改以计算 int 返回值然后使用

  int TakeFoo(const Foo& FooBar)
{
Foo FooBar MyFooBar = FooBar;
//使用MyFooBar执行某些操作并返回int
}

p>

  int TakeFoo(Foo FooBar); 

回答您的第一个问题:



int TakeFoo(FooBar)不应该更改FooBar以计算 int 结果。
更好的设计是

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


I have a class Foo. Foo has a few non-const methods. I am okay with calling non-const methods on a temporary Foo object; I am only interested in what the methods actually return, than what they do to the Foo object itself.

First question: Does this, by itself, necessary indicate that the class Foo is not well-designed?

Second question: If I want to continue with Foo as-is, but I still want to be able to pass Foo objects by reference to functions that will call non-const methods on it, what would be the best way to do it?

This is what I arrived at:

// 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);
}

The alternative approach is just doing this:

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

But this approach has the problem that you may be const-cast'ing away the const on an object that was actually declared const, which would put me in undefined-behavior-land.

Edit:

Code examples that use TakeFoo:

Foo GimmeFoo() { ... }

cout << TakeFoo(GimmeFoo()) << endl;

Foo ConstructedFoo(...);

cout << TakeFoo(ConstructedFoo) << endl;

// Continue to use ConstructedFoo

解决方案

Answer to your second question:

If your function TakeFoo is meant to call non const members of Foo then use

int TakeFoo (Foo& FooBar);

If you are sure that TakeFoo only takes an rvalue as argument then use

int TakeFoo (Foo&& FooBar);

If you want to do some changes to Foo in order to compute the int return value then use

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

Or

int TakeFoo (Foo FooBar);

Answer to your first question:

int TakeFoo (FooBar) should not change FooBar in order to compute the int result. A better design would be

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

这篇关于如何覆盖“对于临时对象没有非常量引用”正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆