Function calling after a certain type is forcibly converted in the memory area

Source: Internet
Author: User

1.

The structure of Class A and Class A is arbitrary.

# Include <iostream>

# Include <string>

Calss

{

......

};

Class D and Class D have a member function, which does not operate any member data.

Class D

{

Public:

Void show ()

{

STD: cout <"This's d" <STD: Endl;

}

};

In Main, forcibly convert an instance of A to D

A;

Void * p_t = &;

D * P_d = (D *) p_t;

Then call the member function of D.

P_d-> show ();

In this case, no errors are reported for the compiler and runtime, and the running result is normal.

2.

Add static variables to Class D

Static int s_num;

Static string s_name;

Initialize static variables in the global environment. (Static variables of a class cannot be initialized within the class. They should be initialized in the form of classname: Para in the global scope .)

Class D

{

......

}

Int D: num = 1234;

String name = "class_d ";

Modify the show () method of class D

Void show ()

{

STD: cout <"This's d." <STD: Endl;

STD: cout <"the static number is" <s_num <STD: Endl;

STD: cout <"the static string is" <s_name <STD: Endl;

}

In this case, the compiler and runtime will not report errors and the running results will be correct.

3.

Add non-static member variables to Class D

Int num;

Initialize num In the constructor of D.

D (void)

{

Num = 1000;

}

Add a line to the show () method of class D.

STD: cout <"the number is" <num <STD: Endl;

Run again.

No error is reported for the compiler and runtime, but the result num value is incorrect.

Conclusion

Compiler and runtime allowProgramThe worker forcibly converts a memory area to a certain type of instance, and can use the method that does not manipulate non-static member data in the worker class of the Instance (this method does not manipulate member data, or the manipulated member data is static data), and the correct result is returned.

Methods for manipulating non-static member data in a class include: static functions of the class; common non-static member functions of the class, and non-static member functions of the class without modifying the class are not used in the function.

Cause analysis:

Static variables are stored in the global public area, and non-virtual member functions of the class are also stored in the public area.

The data of an instance of the class in the memory occupied area includes: non-static member data of the class; if the class has a virtual function, there is a virtual function list.

If a member function does not access non-static variables, calling this non-virtual member function only needs to access the public region (both static variables and non-virtual functions are in the public region ). It does not need to access the memory area occupied by the instance of this class, and does not require whether the memory area meets the data format of this class. The result of function execution must be correct.

If the member function accesses non-static variables, the program will forcibly parse the region according to the data format of the class, and the data in it is naturally wrong.

If a member function is a virtual function, it can be inferred and verified. -_-B

Extension:

If void * p_t does not point to a region of another type, it is null. -- Reasoning and verification. -_-B

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.