C ++ common objects of complex objects
This type of object is a simple and complex object.
A constant object is a constant type object, that is, an object constant. The format is as follows:
<类名> Const <对象名> ( <初始值> )
Or <类名> Const <对象名> ( <初始值> )
When defining a constant object, you must assign an initial value to the constant object, and the object is no longer updated.
In C ++ programming, pointers to common objects or references to common objects are often used as function parameters. In this way, the pointer or reference can be used as a function parameter to improve the running efficiency, and the parameter value of the called function will not be changed in the called function, thus improving the system security.
A common pointer (pointer to a Common Object) is an example of a function parameter:
# Include
Using namespace std; class M {public: M (int I) // defines the single-parameter constructor {m = I;} int returnm () const // defined regular member function {return m;} private: int m ;}; int fun (const M * m1, const M * m2 ); // description of the function whose parameters are regular pointers int main () {M m3 (77), m4 (9); int k = fun (& m3, & m4 ); // The real parameter is the object's address value cout <
Returnm ()/m2-> returnm (); // reference the constant member function return mul in the class through a constant object ;}
Program Analysis: in a program, a common object can only reference a common member function. Therefore, the output result is 8.
Example of a program that uses a common object as a function parameter:
# Include
Using namespace std; class M {public: M (int I) // defines the single-parameter constructor {m = I;} int returnm () const // defined regular member function {return m;} private: int m ;}; int fun (const M & m1, const M & m2 ); // The description of the function that is often referenced is int main () {M m3 (7), m4 (9); int k = fun (m3, m4 ); // The real parameter is the object name cout <
The output result is 63.