The sub-class pointer of C ++ points to the Code instance of the parent class, And the pointer instance

Source: Internet
Author: User

The sub-class pointer of C ++ points to the Code instance of the parent class, And the pointer instance

1. Reference a base class object directly with a base class pointer

2. Directly reference the object of the derived class using the pointer of the derived class

3. Use a base class pointer to reference a derived class object. Because the derived class object is also a base class object, this reference is safe,

However, you can only reference base class members. The compiler reports a syntax error if you try to reference only the Members in the derived class through the base class pointer. (The answer to this question is virtual functions and polymorphism)

4. Use the pointer of a derived class to reference the objects of the base class. This reference method may cause syntax errors. The pointer of a derived class must be forcibly converted to the base class pointer first. This method is not safe.

In the depth of Hou Jie, the important nature of Chapter 2 C ++ in MFC is as follows:

1. If you use a "base class Pointer" to point to a "derived class Object", you can only call the function defined by the base class through this pointer.

2. If you direct a "pointer to a derived class" to a "base class Object", you must first make an explicit Transformation Operation (explicit cast), which is very dangerous.

3. If both the base class and the derived class define "functions with the same name", then when the member function is called through the object pointer, the function is actually called, it must be determined by the pointer's original type, rather than the type of the object actually referred to by the pointer, which is essentially the same as the 1st point.

# Include <iostream> # include <stdlib. h> using namespace std; class A {public: char str [20]; void f () {cout <"class A" <endl;} void fff () {cout <"class A's str" <str <endl;} void add () {cout <"class A address: "<this <endl ;}}; class B: public A {public: int I; char sb [20]; B () {cout <"class B constructor is run. "<endl ;}~ B () {cout <"class B destructor is run. "<endl;} void f () {cout <" class B "<endl;} void ff () {cout <"class B" <I <str <sb <endl;} void add () {cout <"class B address: "<this <endl ;}; int main (int argc, char * argv []) {// declare the parent class object A B; // declare the pointer to the parent class object, pointing to the parent class Object A * pa = & B; pa-> add (); // This statement is correct, but it is dangerous, because whether pa points to B or A is uncertain. if we can clearly know that // pa is directed to Class B objects, as we write now, there is no problem. If pa points to Class A objects, there will be A // risk. changing the pointer type does not affect memory allocation, and neither calls constructor B * pb = (B *) p A; // after the type is forcibly converted, it points to the same address. however, data members are accessed Based on the Conversion Type. // member functions belong to Classes rather than objects. member functions of shared classes for each object. after being forcibly converted to B type, you can // call the member functions of the class. After pb-> add () is compiled, the Code only imports a pb pair when calling the add function. // this pb-> add (); // because pa points to the address of the parent class object, the pointer is forcibly converted to the pointer of the derived class, and the more memory is accessed, the more insecure it is. pb-> I = 100; char dsd [100]; strcpy (pb-> sb, "class B's sb. "); strcpy (pb-> str," class A's str. "); // pb-> f () the specific function called depends on the original type of the pointer, rather than the type of the object actually referred to by the pointer. if it is a virtual function, pb-> f (); pb-> ff (); pb-> fff () depending on the actual object type (); system ("PAUSE"); return 0 ;}

Related Article

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.