Is there an overload between the base class and the derived class with the same name function?

Source: Internet
Author: User

A friend of the 2nd Academy posts on BBS:

The following about A derived class exists with the same name function in the base class Fn:Code:class A {public:void fn () {} void fn (int A) {}};      Class B:public A {public:void fn () {}};       int main () {b b;       B.fn (3);   return 0; }

His question is:

1, the above code compilation why cannot pass. (The problem is in line 21st, and the compiler complains that in B, there is no function of FN (int).)

2. What is the benefit of the compiler doing this (that is, not allowing this code)?

I believe this is a very common problem, in many classic C + + books, will be listed as an important C + + problem, detailed in-depth explanation. I can only answer briefly here, may have the same question of the classmate, have a quick understanding of the role. Due to travel outside, very inconvenient, answer the question can not be detailed debugging, also can not do the necessary Search Classics action (no book at hand), make mistakes, please correct me, I will revise in time.

The answer is as follows:


You are involved in an important point of knowledge in C + +. That is, the overloaded action of a function of the same name occurs only between free functions (that is, non-members) and functions within the same class/struct. You cannot span a base class and a derived class. When a derived class writes a function with the same name as the base class, regardless of the same or a different argument list, the action that occurs at this time is called "Overwrite." overriding means that a function of the same name as the base class, within a derived class, becomes impossible to invoke directly (but can be called indirectly).

First of all, we still focus on the nature of the problem, simplifying the code, discarding the irrelevant side. code:struct a   {       void foo (int d)         {           cout <<  "A::foo  - int " << endl;           cout  << d << endl;       }  };      struct b : public a   {       void  Foo (double d)  //covered A::foo (int d);       {            cout <<  "B::foo - double"  <<  endl;           cout << d <<  endl;       }  };   &NBSP;&NBSp Int main ()    {          A a;        a.foo (Ten);             B b;        b.foo (10.2);       b.foo (2);  //call is still B::foo, Although 2 is obviously an integer                       return 0;  }  

The above code, after running the output will be roughly as follows: (Note for add content)
A::foo-int

b::foo-double
10.2
b::foo-double//Call is still B::foo, although 2 is obviously an integer
2

So, how do you call the base class foo (int)? The

method has two types, one of which is "temporary law": code:b b;   B.a::foo (2);  //explicitly calls the foo   within a range


The second is called "Lifelong law", haha this name is my blind, better known as the "snake into law", do not break. Recall the three usages of namespace, one of which is called a "using declaration/declaration", where similar code can be used (in many cases, the class/struct domain, and a namespace have the same functionality). See Code:
  code:struct b : public a   {        using a::foo; //introduces a::foo......             through "use statement"  void foo (double d)        {            cout <<  "B::foo - double"  << endl;            cout << d << endl;        }  };  


Now to invoke: Code:int main (void) {b b; B.a::foo (3); Call the ...    Of course, A::foo (int) B.foo (2); Call the ... also A::foo (int) b.foo (10.234); Call the ...   B::foo (double) return 0; }

Answer the question "What are the benefits of the compiler doing this?"


This is to avoid "non-malicious errors". This is also an important principle in C + + language design: Grammar rules, will try to let programmers avoid "unintentional error", but do not go to "intentional, malicious, bad intentions of the wrong" (click to see this example of this site).

For example, imagine that if I am the author of A, you are the author of B.

because Foo is not a "virtual/virtual" function, it is possible to write between the two. If at first I didn't write the foo (int) function in a, and you wrote a foo (double) function in B because of the need, and it was nice to use----because there is no such function in the base class, you can write Foo (2) or Foo (2.0). Since the conversion of int to Double is safe, it is very nice to call B's own foo (double) twice. Usually that's what we want.

Then one day, as a member of the development team, I thought I needed a void foo (int) When I modified a, so I added this function to a. And because it's not virtual, the main thing is this: because I am the author of the base class, I have no control over how many people in the world have derived my class A. That's why I'm too lazy to tell you. There is a common function in Class A. But what is the situation now? If the common name of a function of the derived class and the base class can also constitute an overloaded relationship, finished, when you get the new version of A, recompile the project, everything is OK, the compiler does not report any errors, but you said earlier that the code is suddenly changed to call the base class, I just wrote the same name function, which is also good.

Usually, the authors of the base class are the people who are quite good, why. Because he is shouldering more responsibility. When a class is provided in the form of a "base class", usually it should not--some students rob the words: "It should not be modified"-that is not (we are not writing COM components), the base class also to develop, there are version upgrades, otherwise the class library how to make progress. The correct requirement is: Usually, the changes it makes should be forward compatible. That is, the modification of the base class, you can provide more new functionality for derived classes, but should not affect the original functionality of the derived class. In this request, if C + + in this case the rule is: Write the base class only according to their own needs, write an ordinary member function, resulting in the original behavior of the derived class was secretly modified, it is too difficult for the base class author, he will eventually every write a function, all hard to guess there will be an Asian, In the Americas, the author of a derived class on the Antarctic continent, in the past, or now, or in the future, writes a function that has exactly the same name, which is too tiring. The author of the base class is no more bull, but you shouldn't have pushed him into it.

The father of C + + is wise. OH yeah~~~

-------------------------------------

If you want to communicate with me, please click on the link below to become my friend:
http://student.csdn.net/invite.php?u=112600&c=f635b3cf130f350c

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.