函数重载的乐趣 [英] Fun with function overloading

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

问题描述

我之前曾问过这个问题,但输入的代码错误.我不知道如何修改它,所以我删除了它并重新发布了它.如果这还没有完成,我深表歉意.

I asked this question previously but mistyped the key line of code. I couldn''t figure out how to modify it so I deleted it and am reposting it. My apologies if this is not the done thing.

class Thing
{
public:
   Thing() {}
...
};

class Blah
{
public:
  const Thing* GetThing() const;

private:

   Thing* GetThing(); 
};


void anotherclass::function(Blah& b)
{
   const Thing* thing = b.GetThing();
}



给出错误无法访问私有成员".

谁能告诉我为什么?

如果我将b设为const Blah&很好,但我只是想知道为什么VS无法解决对GetThing()的公共const版本的调用?



Gives the error ''Cannot access private member''.

Can anyone tell me why?

If I cast b to be const Blah& it''s fine but I''m just interested why VS doesn''t resolve the call to the public const version of GetThing()?

推荐答案

anoterclass::GetThingBlah&不是const.
因此,b.GetThing()解析为存在的 non-const 函数,但是它是私有的.

将函数设为私有会使它不可访问,但不会将其从声明范围中删除.

试试

The parameter of anoterclass::GetThing is Blah& that is not const.
Hence b.GetThing() resolve to the non-const function, that exist, but it is private.

Making a function private makes it not accessible, but doesn''t remove it from the declaration scope.

try

void anotherclass::function(const Blah& b)
{
   const Thing* thing = b.GetThing();
}


如果将b强制转换为const Blah&,则只能调用声明为const的方法,即不能修改对象内部状态的方法.在这种情况下,选择const Thing* GetThing() const.

但是,如果您输入以下内容,它将继续起作用:

If you cast b to const Blah& then you can call only methods declared as const, i.e. that cannot modify the internal state of the object. In this case the choice is for const Thing* GetThing() const.

But it will continue to work if you write:

class Thing
{
public:
   Thing() {}
...
};

class Blah
{
public:
  Thing* GetThing() const;

private:

   Thing* GetThing();
};


void anotherclass::function(Blah& b)
{
   const Thing* thing = b.GetThing();
}



这将继续起作用,因为可以安全地将const属性隐式添加到变量中,而相反则不安全.

在代码中,编译器根据b是否为const的事实来选择要使用的方法的版本.



This will continue to work because is safe to implicitly add the const attribute to a variable, whereas it is not safe the opposite.

In your code, the compiler choose which version of your method to use depending on the fact that b is const or not.


感谢那些回答.

从c ++规范...

13.3.1.4:
对于非静态成员函数,隐式对象参数的类型是对cv X的引用,其中X为
该函数是成员的类,而cv是成员函数中的cv-qualification声明-
tion. [示例:对于类X的const成员函数,假定额外参数的类型为ref-
const X的含义.]

然后根据13.3.3.1.1,const变体将被视为转换",因此更不合适.
Thanks to those who replied.

From the c++ spec...

13.3.1.4:
For non-static member functions, the type of the implicit object parameter is reference to cv X where X is
the class of which the function is a member and cv is the cv-qualification on the member function declara-
tion. [Example: for a const member function of class X, the extra parameter is assumed to have type ref-
erence to const X. ]

Then according to 13.3.3.1.1, the const variant would count as a "conversion", and thus be a worse fit.


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

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