最烦人的解析
我从
class Timer {
public:
Timer();
};
class TimeKeeper {
public:
TimeKeeper(const Timer& t);
int get_time()
{
return 1;
}
};
int main() {
TimeKeeper time_keeper(Timer());
return time_keeper.get_time();
}
从外观上看,它应该因行而导致的编译错误:
TimeKeeper time_keeper(Timer());
但是只有在存在retoce> retod> retocer_keeper.get_time();
时才发生。
为什么这条线甚至很重要,编译器会在time_keeper(timer())
构造上发现歧义。
I got the code from here.
class Timer {
public:
Timer();
};
class TimeKeeper {
public:
TimeKeeper(const Timer& t);
int get_time()
{
return 1;
}
};
int main() {
TimeKeeper time_keeper(Timer());
return time_keeper.get_time();
}
From the looks of it, it should get compile error due to the line:
TimeKeeper time_keeper(Timer());
But it only happens if return time_keeper.get_time();
is present.
Why would this line even matter, the compiler would spot ambiguity on time_keeper(Timer() )
construction.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是由于
timekeeper time_keeper(timer());
被解释为函数声明而不是变量定义。这本身不是一个错误,但是当您尝试访问time_keeper的get_time()
成员时(这是一个函数,而不是TimeKealer实例)时,您的编译器会失败。这就是您的编译器查看代码的方式:
This is due to the fact that
TimeKeeper time_keeper(Timer());
is interpreted as a function declaration and not as a variable definition. This, by itself, is not an error, but when you try to access theget_time()
member of time_keeper (which is a function, not a TimeKeeper instance), your compiler fails.This is how your compiler view the code: