如何用CONST调用函数 [英] How to call function with CONST

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

问题描述

我最近观察到,一个具有相同名称和相同原型的两个函数的类可能仅在常数上有所不同



i recently observed that it is possible for a class having two functions with same name and same prototype which differs only in const-ness

class A
{

public:

void PrintMessage()
{
  printf("void PrintMessage()");
}
void PrintMessage() const
{
  printf("void PrintMessage() const");
}


}


A a;
a.PrintMessage();  ///Here function without const get called 









我的问题是,如何用const调用函数??





My question is , how to call function with const ??

推荐答案

将在类的const对象上调用const方法。所以要调用const方法:



The const method will be called on const object of the class. So to call the const method:

const A a;
a.PrintMessage();


首先,在此上下文中只需 const 表示调用不应修改对象的状态。显然,它仅对实例(非静态)功能有意义。



我对这种病理学感兴趣并进行了实验。令我惊讶的是,我的Microsoft编译器编译了它,即使有了这个调用。我期望类声明将编译,但尝试调用该方法不会。我错了。



现在,调试显示非常量函数总是为非常量对象调用,而常量函数是常量对象。



对我而言,根本没有任何意义;并且唯一合理的行为是编译失败。也许,你设法找到标准的弱点( Rahul证实这在标准中有描述;谢谢你,Rahul)。这就是问题所在:如果一个类的实例在非常量中,那么常量函数仍然被成功调用,很自然。但是如果你根本不改变任何东西,只是添加一个具有相同名称/签名的非常量函数,它会窃取对自身的调用。我认为这样的编译器行为非常危险。 (但是,C ++有很多危险的功能,这个只是另一个小功能。)



无论如何,在实践中,我建议你避免这种情况无论如何。 C ++需要一些纪律...



可能所谓的C ++层可以找到一些借口,我不知道。无论如何,我很乐意考虑这样的论点。



而且我不在乎:我从来没有对C ++有任何信任,在我看来,它带来了对编程和文化的危害更大,然后受益。 (拜托,C ++主张,没有火焰战争......不是很有趣;这是我个人的意见,我不打算讨论太多......)



但是我打算为这个问题投票5!



-SA
First of all, const in this context simply means that the state of the object should not be modified by the call. Apparently, it makes sense only for instance (non-static) functions.

I took interest in this pathology and experimented. My Microsoft compiler compiled it, even with the call, to my surprise. I expected that the class declaration will compile, but an attempt to call the method will not. I was wrong.

Now, debugging shows that the non-constant function is always called for non-constant object and a constant function for a constant object.

To me, it makes no sense at all; and the only reasonable behavior would be to fail compilation. Perhaps, you managed to find a weakness in the standard ( Rahul confirmed that this is described in the standard; thank you, Rahul). Here is the problem with that: if an instance of the class in non-constant, the constant function is still successfully called, quite naturally. But if you don''t change anything at all but only add a non-constant function with the same name/signature, it "steals" the call to itself. I think such compiler behavior is very dangerous. (However, C++ has so many dangerous features, that this one is just yet another small one.)

Anyway, in practice, I would recommend you to avoid this situation by all means. C++ requires some discipline...

May be so called C++ layers can find some excuse for that, I don''t know. Anyway, I would gladly consider such arguments.

And I don''t care too much: I never had any faith in C++, in my opinion, it brought a lot more harm to programming and culture then benefits. (Please, C++ advocates, no flame wars... not really interesting; this is my personal opinion which I am not going to discuss much...)

But I''m going to vote 5 for this question!

—SA


技术这种行为背后的原因是重载决议。

方法声明为

The technical reason behind this behaviour is the the overload resolution.
Method declared as
class A {
  void PrintMessage()       { ... }
  void PrintMessage() const { ... }
}



翻译幕后 to(这是概念性的,你无法看到或称之为):


translate behind the scenes to (this is conceptual, you can not see or call that):

static void A::PrintMessage(A       * const this) { ... }
static void A::PrintMessage(A const * const this) { ... }



如果C ++编译器必须在函数声明之间进行选择,重载决策采用最佳匹配


If the C++ compiler has to choose between function declarations, the overload resolution takes the best match:

A varA = A();
varA.PrintMessage();
const A constA = A(); // BTW: this is identical to A const constA = ...
constA.PrintMessage();



这会在幕后内部翻译成:


this translates internally behind the scenes again into:

...
A::PrintMessage(&varA);   // best match for &varA of type A* is PrintMessage(A*const)
...
A::PrintMessage(&constA); // best match for &constA of type A const* is PrintMessage(A const*const)



从技术角度来看:直接 - 没有(其他)魔法参与;-)

干杯

Andi


From a technical point of view: straight forward - no (other) magic involved ;-)
Cheers
Andi


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

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