C ++ accesses the private functions of the parent class from the subclass

Source: Internet
Author: User
C ++: access the private functions of the parent class from the subclass.

Transferred from longyin pavilion Co., http://blog.sina.com.cn/dragonsound. Thanks to the work of the original author

(22:44:21)


Tags: virtual function c private f1 it as we all know, arrays of c and c ++ are insecure, Because array boundary check is not provided for both c and c ++, this makes Array Overflow possible. In a sense, c and c ++ are a language that lacks supervision. However, this is the charm of c and c ++. C ++ gives programmers greater freedom. Compared with JAVA programming, c ++ programmers have greater power and more opportunities to play with some skills, for example, the sub-class calls the private function of the parent class. From the subclass to call the private function of the parent class? Did you hear me wrong?

Of course not!

Although the information we get from various c ++ books is that the Child class inherits only the protected and public members from the parent class, the private Members of the parent class cannot inherit the quilt class, but when the private function of the parent class is a virtual function, we can read the information in the VTABLE table to find the address of the parent class virtual function, then call it.


Let's first recall how c ++ polymorphism is implemented.

When the virtual keyword appears in the c ++ class, the class has a VTABLE. The VTABLE contains the offset of each virtual function in the virtual memory. That is to say, if Class A has three virtual functions: f1, f2, f3, in the virtual function table VTABLE of Class A, the offset addresses of f1, f2, and f3 will be stored in sequence.

Then, when class A only has one virtual function, Class A occupies two or three memories, that is, sizeof (A) and Class, is there A difference between sizeof (A) and more virtual functions? No, no difference. If all the members of A class are virtual-modified virtual functions, when you use sizeof (A) to view its size, the result is 4 without exception -- in A 32-bit system, the four bytes are exactly the size of an integer and the size of a pointer.

Why?

For Class A, it does not need to know how many virtual functions there are. It only needs A pointer to VTABLE, which is usually called vptr. It points to VTABLE. As to how many virtual functions are there, you only need to find them in VTABLE. Let's imagine that vptr is a road sign pointing to a company named VTABLE. As for how many employees there are in the company, you must enter VTABLE to know.


Now, let's summarize:

Conclusion 1: in a class with virtual functions, there must be a vptr pointing to the VTABLE

Conclusion 2: VTABLE stores the offset addresses of each virtual function in the virtual memory in sequence.

Now, let's introduce the other two c ++ rules.

Rule 1: in any class, vptr must be stored in the first four bytes of the class instance.

Rule 2: when both the subclass and the parent class have virtual functions, the VTABLE of the subclass also has the virtual function offset addresses of the parent class and the Child class, in addition, the virtual function offset address of the subclass must be appended to the parent class virtual function offset address.

That is to say, if there are two classes:

Class {

Private:

Virtual void WhoAmI (){

Cout <"I am class A" <endl;

}

};


Class B: public {

Public:

Void WhoAmIForB (){

Cout <"I am class B" <endl;

}

};


Then, the instance

A;

B B;

Each has a vptr, where the vptr of a is (int *) (* (int *) (& a), and the vptr of B is (int *) (* (int *) (& B )),

The two vptr points to their respective vtables. The content stored in the VTABLE of the parent class A is the offset address of A: WhoAmI, and what about the VTABLE of the subclass B?

In the VTABLE of subclass B, the offset addresses of A: WhoAmI and B: WhoAmIForB are stored in sequence.


Note that the key is here: A's virtual functions are all private, isn't it? However, the compiler linker seems to have forgotten the keyword private at this time. No matter whether these virtual functions are private or public, their offset addresses are stored in the VTABLE of the subclass without exception!

This is a flaw! You can kill your life!


Since we know the vptr of the subclass instance, why cannot we calculate the VTABLE of the subclass?

Since we know the VTABLE of the subclass, according to Rule 2, why cannot we calculate the virtual function offset of the parent class?

The answer is: the offset of the first virtual function of the parent class is (int *) (* (vptr of the subclass), that is, -- (int *) (* (int *) (* (int *) (& B ))).

When we forcibly convert a pointer to a function, we can call it to call the parent class private function from the subclass.


Run the following code:

# Include <iostream>


Using namespace std;


Class {

Private:

Virtual void WhoAmI (){

Cout <"I am class A" <endl;

}

Virtual void f0 (){

Cout <"This is f0" <endl;

}

Virtual void f1 (){

Cout <"This is f1" <endl;

}

};


Class B: public {

Public:

Void WhoAmIForB (){

Cout <"I am class B" <endl;

}

};


Typedef void (* FUNC )();


Int main (int argc, char * argv [])

{

B B;


B. WhoAmIForB ();

// B. WhoAmI (); error C2248: "A: WhoAmI": unable to access the private member (declared in the "A" class)




FUNC func = (FUNC) (int *) (* (int *) (& B ))));

Func ();


Return 0;

}

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.