作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
让我们考虑这段代码:
struct message
{
uint8_t* data;
size_t length;
};
class device_base
{
// ...
public:
virtual ssize_t exec(uint8_t cmd, const uint8_t* data = nullptr, size_t length = 0);
inline ssize_t exec(uint8_t cmd, const message& msg)
{
return exec(cmd, msg.data, msg.length);
}
// ...
};
class device : public device_base
{
// The exec method do not overloaded or overridden here.
};
class device_uart : public device
{
// ...
public:
ssize_t exec(uint8_t cmd, const uint8_t* data = nullptr, size_t length = 0);
void some_method(const message&);
// ...
};
// ...
void device_uart::some_method(const message& msg)
{
// exec(SOME_COMMAND, msg); // The inline method device_base::exec is invisible here by some reason.
device::exec(SOME_COMMAND, msg); // OK.
device_base::exec(SOME_COMMAND, msg); // OK too.
exec(SOME_COMMAND, msg.data, msg.length); // OK, of course.
}
为什么在device_uart
类中没有看到内联非虚方法exec
?
最佳答案
Why the inline non-virtual method
exec
is not seen in thedevice_uart
class?
这是一种“隐名”; device_uart
类的成员函数中,device_base::exec
被隐藏了,因为device_uart类中有一个同名方法
本身。函数不能通过不同的范围重载。exec
根据unqualified name lookup的规则:
name lookup examines the scopes as described below, until it finds at least one declaration of any kind, at which time the lookup stops and no further scopes are examined.
这意味着名称 exec
将在 device_uart
的范围内找到,然后名称查找停止,基类中的名称将不会被考虑用于重载解析完全没有。
要解决这个问题,可以使用作用域解析运算符::使其成为qualified name lookup正如你所展示的;或者您可以使用 using
将名称引入同一范围,重载决议将按预期生效。例如
class device_uart : public device
{
// ...
using device_base::exec; // introduce the names from base class into the same scope
public:
// ...
};
关于c++ - 为什么在没有显式范围解析的情况下无法访问父类的父方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41776297/
我是一名优秀的程序员,十分优秀!