如何调用const重载函数的特定变量? [英] How can I call a specific variant of a const-overloaded function?

查看:90
本文介绍了如何调用const重载函数的特定变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有成功在网上找到帮助,但我也很难简明地定义我的问题,所以我将从代码开始:



I had no success finding help on the web, but I also have some trouble to concisely define my problem, so I'll start with the code:

const cHalfEdge* cHalfEdge::prev() const
{
   const cHalfEdge* result = nullptr;
   if (next_ != nullptr) {
      const cHalfEdge* current = this;
      while (current->next_ != nullptr && current->next_ != this) {
         current = current->next_;
      }
      if (current->next_ == this)
         result = current;
   }
   return result;
}

cHalfEdge* cHalfEdge::prev()
{
   const cHalfEdge* result = (const cHalfEdge*)(this)->prev();
   return const_cast<cHalfEdge*>(result);
}



正如你所看到的,我有两个相同函数的重载版本,它们各自的const限定符不同。我故意做了两个函数来确保const正确性。由于实现是相同的,我希望它只在一个地方。



现在问题是我不能轻易地从另一个版本调用一个版本。在上面的代码中,我做了一个小的黑客来强制调用另一个版本,即使我还没有运行该程序,我相信它的工作原理。然而,编译器(VC10.0)发出警告,声称此函数将导致无休止的递归。



我意识到还有其他可能的解决方案,例如: G。将实现移动到具有不同名称的函数。但这不是我想讨论的问题



我总是试图避免类型转换,而第一个特别难看。我想知道它是否真的有必要 - 这就是我的问题所在:



是否有可能专门调用重载函数的const版本而不进行转换我把它称为const的对象?



编辑:

改进了我的问题的措辞并尝试另一个搜索,我在这里找到了类似的讨论:

http://stackoverflow.com/questions/5620071/how-do-i-call-overloaded-member-function-with-cv-qualifier [ ^ ]

看起来确实需要const-cast对象(在这种情况下, this )。可惜,我希望有一个更优雅的解决方案。


As you can see, I have two overloaded versions of the same function, that differ only in their respective const qualifier. I deliberately made two functions to ensure const-correctness. Since the implementation is the same however, I want it to be in only one place.

Now the problem arises that I cannot easily call one version from the other. In the code above, I did a small hack to force the call of the other version, and even though I haven't even run the program yet, I am sure that it works. Still, the compiler (VC10.0) issues a warning, claiming that this function will cause an endless recursion.

I realize there are other possible solutions, e. g. to move the implementation to a function with a different name. But that is not the problem I wish to discuss.

I always try to avoid type casts, and that first one is particularly ugly. I wondered if it is really necessary - And that is where my question arises:

Is it possible to specifically call the const version of an overloaded function without casting the object I'm calling it on to const?


After improving the wording of my question and trying another search, I've found a similar discussion here:
http://stackoverflow.com/questions/5620071/how-do-i-call-overloaded-member-function-with-cv-qualifier[^]
Looks like it is indeed necessary to const-cast the object (this, in this case). Pity, I would have hoped for a more elgant solution.

推荐答案

你可以跳过强制转换为 const 将某些东西分配给 const 成员对源对象什么也不做(通常)。



所以你可以替换

You can skip the cast to const as assigning something to a const member does nothing (usually) to the source object.

So you could replace
cHalfEdge* cHalfEdge::prev()
{
   const cHalfEdge* result = (const cHalfEdge*)(this)->prev();
   return const_cast<cHalfEdge*>(result);
}



with


with

cHalfEdge* cHalfEdge::prev()
{
   const cHalfEdge* const_this = this;
   return const_cast<cHalfEdge*>(const_this->prev());
}





删除明确(const cHalfEdge *)演员。



你总是要在结果上抛弃 const



希望这会有所帮助,

Fredrik



That removes the explict (const cHalfEdge*) cast.

You will always have to cast away the const on the result though.

Hope this helps,
Fredrik


我认为你在操作员问题上遇到了麻烦。你打算做的是:

I think you are stumbling over an operator issue. What you intended to do was:
const cHalfEdge* result = ((const cHalfEdge*)this)->prev();







我不认为有直接的方法指定你想要调用函数的const重载,除了使转换更好一点:




I don't think that there is a direct way to specify that you want to call the const overload of the function, except making the cast a little nicer:

const cHalfEdge* result = const_cast<const cHalfEdge*>(this)->prev();


这篇关于如何调用const重载函数的特定变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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