In-depth understanding of the inheritance of C ++ data members
Recently, I encountered this problem in my work, that is, all subclasses must have a data member. to restrict the interface, the best way is to put the data member in the base class and use protected. First, use the code to describe my problem:
class BaseB;class BaseA{Base();virtual ~Base();//...other operationprotected:BaseB *m_p;}
In fact, it is very simple to say that basea and baseb are both base classes, and basea has a baseb pointer, because we need to use the baseb polymorphism feature in future subclasses.
In C ++, almost all c ++ features such as polymorphism are implemented through virtual function tables.
How is the inheritance of data members realized? What is the memory storage of classes and class objects? To address this issue, let's take a look at a small piece of code:
#include <stdio.h>#include <iostream>using namespace std;class Base{public:Base(){}void operation(){}protected:char a;int b;};class A: public Base{public:short c;};int main(int argc, char **argv){A obj;cout << "the size of the obj is " << sizeof(obj) << endl;}
The output result is: the size of the obj is 12.
It is easy to analyze the 12 Bytes: 1 + 3 + 4 + 2 + 2 = 12
Actually
struct _data{ char a; int b; short c;}
The structure defaults to the size of the 4-byte memory.
This Code contains at least two points:
1. non-virtual functions do not occupy class object space. This is a good explanation. Non-virtual functions are not run time, which means that the function address is known in the compilation phase, during compilation, the compiler has replaced the function call with the function entry address. Member functions are not stored in the class. When there are other virtual functions, the pointer of the virtual function table occupies only four bytes. This is why virtual functions increase the class space.
2. The derived class copies the data inherited from the base class. In C ++, the inheritance of data members is to copy the data inherited from the base class to the derived class. On the other hand, the class can completely replace struct without increasing the object size. This includes myself. Sometimes, when writing data structures in C ++, I often use struct to avoid using class. I always think that class will increase the occupied space. This is a misunderstanding
In the preceding code, the data members of the derived class A are: A, B, and C. The member methods include void operation. The memory distribution of the class objects is as follows:
_____________________________________
| __________ Char a ____________________ _ |
| __________ Int B ___________________ |
| __________ Short C _____________________ |
It can be seen that only data members exist in the derived class object, and a copy of the inherited data member of the base class is copied.
Here, we have basically learned how to implement the inheritance of data members.
Next, let's continue:
# Include <stdio. h ># include <iostream> using namespace STD; class base {public: Base () {A = 0; B = 0 ;} virtual void operation () {B = 12 ;} virtual void print () {} protected: Char A; int B ;}; Class A: public base {public: Short C; void operation () {B = 24 ;} void print () {cout <"result is" <B <Endl ;}; int main (INT argc, char ** argv) {A OBJ; base * P = & OBJ; // cout <"the size of the obj is" <sizeof (OBJ) <Endl; P-> base: Operation (); // Note: The method p-> Print () of the base class is forcibly called here; // use the printp-> operation () of the derived class of the called thing (); // call the Method P-> Print ();} of the derived class ();}
The result is:
Result is 12
Result is 24
Base: Operation () Forcibly calls the method of the base class to modify the data member. In the subclass, we can see that the data inherited from the base class in the subclass also changes through printing. In the process of inheriting data members, although it is a data member inherited from the base class, in the base class and subclass, this data member has only one instance, this means that the data members inherited from the base class and subclass share one copy.