Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to pass an object as a parameter in C++

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to pass the object as a parameter in C++. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

There are two ways to pass parameters on C++:

1. Pass a value (the pointer as a parameter, essentially just passing the address as a value).

two。 Pass the quotation.

Therefore, all transmission methods are either citation or value. Unlike javascript, values are passed if it is a raw data type, and references are passed if it is an object data type.

If the return value of a function is an object, in some cases, replacing "value passing" with "reference passing" can improve efficiency. In some cases, you can only use "value passing" instead of "reference passing", otherwise you will make an error.

For example:

Class String

{⋯

/ / assignment function

String & operate= (const String & other)

/ / Additive function. If there is no friend modification, only one right argument is allowed.

Friend String operate+ (const String & S1, const String & S2)

Private:

Char * m_data

}

The implementation of String's assignment function operate = is as follows:

String & String::operate= (const String & other)

{

If (this = & other)

Return * this

Delete m_data

C programming Guide for High quality Candlespace C, v 1.0

2001 Page 39 of 101

M_data = new char [strlen (other.data) + 1]

Strcpy (m_data, other.data)

Return * this; / / returns a reference to * this without copying the process.

}

For assignment functions, the String object should be returned as "reference passing". If you use the "value pass"

Method, although the function is still correct, because the return statement copies the * this to the external memory that holds the return value

In the storage unit, unnecessary overhead is increased and the efficiency of the assignment function is reduced. For example:

String a,b,c

A = b; / / if you use "value pass", a * this copy will be produced.

A = b = c; / / if you use "value pass", two * this copies will be produced.

The implementation of the addition function operate + of String is as follows:

String operate+ (const String & S1, const String & S2)

{

String temp

Delete temp.data; / / temp.data is a string with only'\ 0'

Temp.data = new char [strlen (s1.data) + strlen (s2.data) + 1]

Strcpy (temp.data, s1.data)

Strcat (temp.data, s2.data)

Return temp

}

For additive functions, the String object should be returned as "value passing". If you use "reference passing" instead

Then the return value of the function is a "reference" pointing to the local object temp. Because temp is automatically used at the end of the function

Destroy, will cause the returned reference to be invalid. For example:

C = a + b

At this time, a + b does not return the expected value, and c gets nothing, leaving a hidden danger.

The above is how to pass the object as a parameter in C++ shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report