In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to solve the practical application problems of C++ object delivery. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
C++ language is very powerful, not only can support a variety of programming, but also has object-oriented characteristics, greatly meet the needs of developers. Here we first take a look at the relevant content of C++ object transmission, you can learn a lot from it.
If the return value of a function is an object, in some cases, replacing "value passing" with "reference passing" in C++ object 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, there can only be one right parameter friend String operate+ (const String & S1, const String & S2); private: char * masked data;}
The implementation of String's assignment function operate = is as follows:
String & String::operate= (const String & other) {if (this = = & other) return * this; delete mdatea; m_data = new char [strlen (other.data) + 1]; strcpy (m_data, other.data); return * this; / / returns a reference to * this without copying process}
For assignment functions, the String object should be returned in the form of "reference passing" in C++ object passing. If you use the way of "value passing", although the function is still correct, because the return statement copies * this to the external storage unit that stores the return value, it increases the unnecessary overhead and reduces the efficiency of the assignment function. For example:
String a this bforce c; ⋯a = b; / if you use "value pass", you will produce a * this copy a = b = c; / / if you use "value pass", you will produce two * this copies of String. The implementation of operate+ is as follows: String operate+ (const String & S1, const String & S2) {String temp; delete temp.data / / temp.data is the string 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, the return value of the function is a reference to the local object temp. Because the temp is automatically destroyed at the end of the function, the returned reference is 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.
On how to solve C++ object delivery practical application problems to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.