简体   繁体   English

为什么类不能为函数和数据成员具有相同的名称?

[英]Why can't a class have same name for a function and a data member?

Why can't a c++ class have same name for a function and a data member? 为什么c ++类不能为函数和数据成员使用相同的名称?

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()'

Suppose you want to take the address of the member-function size() , then you would write this: 假设你想获取成员函数size()的地址,那么你会写这个:

auto address = &demo::size;

But it could be very well be the address of the member-data size as well. 但它也可能是成员数据size的地址。 Ambiguous situation. 暧昧的情况。 Hence, it is disallowed by the language specification. 因此,语言规范不允许这样做。

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. 这并不是说C ++委员会不可能提出解决方案,但我认为这样做没有重大收获。 Hence, the Standard simply disallowed it, to keep things simple. 因此,标准只是禁止它,以保持简单。

Also, the difference between member-data and member-function becomes less distinguishable visually if one declares the member function size() as: 此外,如果将成员函数size()声明为:成员函数size() ,则成员数据和成员函数之间的差异在视觉上变得不那么明显:

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!
}

Output: 输出:

It is crazy! 太疯狂了!

See the online demo : http://ideone.com/ZjwyJ 请参阅在线演示: 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
};

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;
}

Test code: 测试代码:

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

Output: 输出:

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

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


The conclusion... 结论...

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). 1.不幸的是,该语言没有为成员数据提供类似的功能,比如成员数据重载,语言也不提供跨成员重载(允许成员数据和成员函数具有相同的名称 - 问题中的情况)。

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. 此外,当我说“C ++委员会提出解决方案”时 ,我并不是说解决方案已经标准化 ,我只是意味着他们知道编译器如何解决它,以及解决方案有多复杂。

because if you use size in your class somewhere then the compiler does not know what to do. 因为如果你使用size在你的类地方,那么编译器不知道该怎么办。 It can be either the int-data-member or it can be the function-pointer. 它可以是int-data-member,也可以是函数指针。 So the compiler is not able to seperate both kind 因此编译器无法分离这两种类型

As an example (Not maybe the best but it might explain it visually): 作为一个例子(不是最好的,但它可能在视觉上解释):

class Size {
        std::size_t size_;
    public:
        Size(std::size_t s = std::size_t() ) : size_(s){}
        std::size_t operator()() const {
            return size_;
        }
        void operator()(std::size_t s) {
            size_ = s;
        }        
};

class Demo {
    public:
        Size size;
};

int main() {
    Demo d;
    d.size(10);
    std::size_t size = d.size();
    return 0;
}

Basically the variable could be callable as well. 基本上变量也可以调用。 So there is no way for the compiler to know your intentions. 所以编译器无法知道你的意图。 Of course this is defined by the language that it shall not be possible to have the same name as identifier within the same scope. 当然,这是由语言定义的,它不可能与同一范围内的标识符具有相同的名称。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么不能使用与成员类型同名的方法? - Why can't I have a method with the same name as a member type? 为什么内部 class 和外部 class 不能同名? - Why can't inner class and outer class have same name? 为什么静态数据成员不能与非静态数据成员具有相同的名称? - Why can't a static data member has the same name with non-static data member? 一个类可以在同一个命名空间中有一个同名的成员吗? - Can a class have a member that is another with the same name in the same namespace? 为什么成员 function 不能要求同一个 class 的 static constexpr 成员为真? - Why can't a member function require a static constexpr member of the same class to be true? 为什么类方法不能使用相同的名称调用全局函数? - Why can't a class method call a global function with the same name? 为什么一个类成员函数不能接受与其类相同类型的多个参数 - Why can't a class member function take more than one argument of the same type as its class 为什么我不能将指向Derived类成员函数的指针强制转换为类Base? - why can't I cast a pointer to Derived class member function to the same but of class Base? 不能使用与成员函数同名的辅助函数 - Can't use helper function with same name as member function 与同一类的私有数据成员同名的成员函数的变量会发生什么? - What happens to a variable of a member function having same name as that of a private data member of the same class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM