G ++编译器不能区分变量和函数具有相同的名称? [英] G++ compiler cannot distinguish variable and function with the same name?

查看:223
本文介绍了G ++编译器不能区分变量和函数具有相同的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

类方法和变量同名,C ++编译错误,而不是在Java?

当我的类成员名称和成员函数名称相同时,G ++编译器会报错。看来只要变量名恰好与函数名相同,编译器就会抱怨。

The G++ compiler would complain when my class member name and member function name are the same. It seems that whenever a variable name happens to be the same as a function name, the compiler would complain.

在Java中,情况并非如此。我只是想知道为什么G ++编译器不能区分变量名称从函数名称,因为函数名称总是带有一对括号。

In Java, it is not the case. I just wonder why the G++ compiler cannot distinguish a variable name from a function name since the function name always comes with a pair of parenthesis.

推荐答案

struct Callable
{
    void operator()() const { }
};

struct Test
{
    void Call() { }
    Callable Call;
};

int main()
{
    Test x;
    x.Call(); // To which 'Call' does this refer?
}

这篇关于G ++编译器不能区分变量和函数具有相同的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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