C ++ common objects of complex objects

Source: Internet
Author: User

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.

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.