Hidden names and countermeasures in inheritance relationships in C + +

Source: Internet
Author: User

In C + + and its object-oriented theory, there is a scenario where a class inherits from another class, and if all two classes have a function name and the same member functions as the parameter and its return value, then the function of the subclass automatically hides the function corresponding to the parent class. The same name is hidden. In the process of development, we want the member function of the subclass and the member function of the parent class to be executed. Then you need to explicitly call a member function with the same name as the parent class in the member function of the subclass.
The following example shows a hidden concept with the same name and a method to avoid shadowing with the same name.

#include <iostream>using namespace Std;class baseclass{public:baseclass () {cout << baseclass::baseclass ( ) \ t "<< this << Endl;} ~baseclass () {cout << "baseclass::~baseclass () \ t" << this << Endl;} void ShowMsg () {cout << "baseclass::showmsg () \ t" << this << Endl;} void ShowMsg2 () {cout << "baseclass::showmsg2 () \ t" << this << Endl;}}; Class Subclassa:public Baseclass{public:subclassa () {cout << "Subclassa::subclassa () \ t" << this << Endl;} ~subclassa () {cout << "Subclassa::~subclassa () \ t" << this << Endl;} void ShowMsg () {baseclass::showmsg () cout << "subclassa::showmsg () \ t" << this << Endl;} void ShowMsg2 () {cout << "subclassa::showmsg2 () \ t" << this << Endl;}}; int main (int argc, char *argv[]) {cout << process begin at\t << (void*) &main << Endl; Subclassa subclassa;cout << "Hide test for base class members" << ENDL;SUBCLASSA.SHOWMSG2 () cout << "===== explicitly call test ======== for hidden base class members" << endl;subclassa.showmsg (); cout << "======end test=========" < < Endl;return 0;}

Log of the program:
Process begin at 0x401a65
Baseclass::baseclass () 0x62feee
Subclassa::subclassa () 0x62feee
Hide tests for base class members
SUBCLASSA::SHOWMSG2 () 0x62feee
===== a test that explicitly calls a hidden base class member ========
Baseclass::showmsg () 0x62feee
Subclassa::showmsg () 0x62feee
======end test=========
Subclassa::~subclassa () 0x62feee
Baseclass::~baseclass () 0x62feee

Hidden names and countermeasures in inheritance relationships in C + +

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.