In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "what are the three easily confused concepts in C++". Many people will encounter such a dilemma in the operation of actual cases. then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
There are two special forms of method (function) calls in JAVA: overloading and rewriting, while C++ adds variables to function calls due to the addition of the virtual function keyword virtual: in addition to overloading, rewriting (also known as override), there is also a hidden saying.
I believe that when most of us write code, we subconsciously avoid these brain teaser-like grammars for the sake of program readability and speed. Try to use different function names to distinguish between base and derived functions (of course, there are a lot of functions that can't be avoided).
But sometimes, some interviewers always like to use these three concepts to embarrass you to see if your C++ foundation is solid. Therefore, in order to interview, we still need to distinguish between these three concepts.
1, overloaded characteristics: in the same class; function name is the same; parameters are different; virtual keyword is optional.
2, the rewrite (override) characteristics are: respectively located in the derived class and the base class; the function name is the same; the parameter is the same; the base class function must have the virtual keyword (this is very important to note).
Personally, I think the above two points are easier to remember, but C++ 's hidden rules suddenly increase the complexity of the problem. The rules are as follows:
1. If the function of the derived class has the same name as the function of the base class, but the parameters are different. At this point, with or without the virtual keyword, the functions of the base class will be hidden (be careful not to be confused with overloading).
2. If the function of the derived class has the same name as the function of the base class, and the parameters are the same, but the base class function does not have the virtual keyword. At this point, the functions of the base class are hidden (be careful not to be confused with rewriting).
With so many tongue twisters, I'm afraid it's not easy to understand. Let's take a simple example:
Class A {public: void f (int a);}; class B: public A {public: void f (char * p)}; void main () {int I = 1; B b; A * pa = & b; B * pb = & b; pa- > f (I); / / call the method of parent A; pb- > f (I); / / here? Which method is called? }
According to habitual thinking, I think that the pointer pb in the main function will call the method of the parent class, in fact, hiding has taken place, and here it meets the hidden condition 1, so here the parent method is hidden, the method of the called subclass, due to the mismatch of parameter types, the compiler reports an error directly!
This is the end of the content of "what are the three easily confused concepts in C++". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 253
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.