为什么类不能为一个函数和一个数据成员使用相同的名字? [英] Why can't a class have same name for a function and a data member?

查看:253
本文介绍了为什么类不能为一个函数和一个数据成员使用相同的名字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么一个c ++类不能为一个函数和一个数据成员命名?

Why can't a c++ class have same name for a function and a data member?

class demo{
    public:
        int size();
    private:
        int size;   
};

int main(){
    return 0;
}


C:\Users\S>g++ demo.c
demo.c:5:7: error: declaration of 'int demo::size'
demo.c:3:7: error: conflicts with previous declaration 'int demo::size()'


推荐答案

假设你想取成员函数 size()的地址,将写入:

Suppose you want to take the address of the member-function size(), then you would write this:

auto address = &demo::size;

但是它可以很好地成员数据的地址 size 。模糊的情况。因此,它被语言规范所禁止。

But it could be very well be the address of the member-data size as well. Ambiguous situation. Hence, it is disallowed by the language specification.

这不是说C ++委员会不可能提出一个解决方案,但我想有没有大的收益。因此,标准简单地禁止它,以保持简单。

That is not to say that it was impossible for the C++ committee to come up with a solution, but I suppose there is no major gain in doing so. Hence, the Standard simply disallowed it, to keep things simple.

此外,成员数据和成员函数之间的差异变得不太可视化 >如果将成员函数 size()声明为:

Also, the difference between member-data and member-function becomes less distinguishable visually if one declares the member function size() as:

typedef void fun_type();

struct demo
{
    fun_type size; //It looks like a member-data, but it's a member-function
};

void demo::size()  //define the member function
{
  std::cout << "It is crazy!" << std::endl;
}

int main()
{
    demo d;
    d.size(); //call the function!
}

输出:

这太疯狂了!

查看在线演示: http://ideone.com/ZjwyJ

现在,如果我们可以如上所述实现成员函数,那么它变得太明显即使是裸眼,您无法使用同样的名称添加其他成员:

Now if we can implement member functions as explained above, then it becomes too obvious even to the naked eye that you cannot add another member with same name as:

struct demo
{
    fun_type size;
    int      size; //error - choose a different name for the member!
};

等待这不完全正确,因为故事还没有完成。有一些不太明显我需要在这里添加。您可以添加多个具有相同名称的成员:

Wait That is not entirely correct, as the story is not finished yet. There is something less obvious I need to add here. You can add more than one member with same name:

typedef void fun_type0();
typedef void fun_type1(int a);
typedef void fun_type2(int a, int b);

struct demo
{
    fun_type0 member;  //ok
    fun_type1 member;  //ok
    fun_type2 member;  //ok
};

这是完全有效的代码,每个成员都是函数 em>不同类型,因此您可以将它们定义为:

This is completely valid code, as each member is a function of different type, so you can define them as:

void demo::member()
{
   std::cout << "member()" << std::endl;
}
void demo::member(int a)
{
   std::cout << "member(" << a << ")" << std::endl;
}
void demo::member(int a, int b)
{
   std::cout << "member(" << a << ", "<< b << ")" << std::endl;
}

测试代码:

int main()
{
    demo d;
    d.member();
    d.member(10);
    d.member(200,300);
}

输出:

member()
member(10)
member(200, 300)

在线演示: http://ideone.com/OM97Q

结论...

相同的名称,只要它们是不同类型的函数。这是通过成员功能重载(或简单功能重载) 1 的功能启用的。

You can add members with same name, as long as they're function of different types. This is enabled by a feature called member-function-overloading (or simple function-overloading)1.

1。不幸的是,语言不为成员数据提供类似的特征,例如成员数据重载,语言也不提供跨成员重载(允许成员数据和成员函数具有相同的名称,问题中的情况)。

1. Unfortunately, the language doesn't provide similar feature, say member-data-overloading, for member data, neither do the language provide cross-member-overloading (that allows member-data and member-function to have the same name — the case in the question).

因此,这里的问题自然会出现:它们不会造成歧义问题吗?是的,他们。但要指出的一点是,C ++委员会提出了解决这个歧义问题的解决方案,因为他们看到巨大的收获(在函数重载的情况下)。

So here a question naturally arises: do they not cause ambiguity problem? Yes, they do. But the point to be noted is that C++ committee came up with a solution to solve this ambiguity-problem, because they saw a huge gain in doing so, (in case of function-overloading).

但问题中的情况仍然含糊不清,因为委员会没有提出解决方案,因为他们没有看到任何巨大的优势注意前面)。此外,当我说C ++委员会想出了解决方案时,我不是指解决方案已经被标准化了 他们知道编译器如何解决它,以及解决方案有多复杂。

But the case in the question remains ambiguous, as the committee didn't come up with a solution, as they didn't see any huge advantage in doing so (as noted before). Also, when I said "C++ committee came up with solution", I do NOT mean that the solution has been Standardized, I merely mean that they knew how the compilers can solve it, and how complex the solution would be.

这篇关于为什么类不能为一个函数和一个数据成员使用相同的名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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