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

What is the temporary target of C++?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "what is the temporary object of C++". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

What is a temporary object?

C++ 's real temporary objects are invisible anonymous objects that do not appear in your source code, but the program does generate such objects at run time.

It usually occurs in the following two situations:

(1) when implicit type conversion is performed in order to make a function call successful.

When an object is passed to a function, and its type is different from the formal parameter type of the function, if the function call can be made successful by implicit conversion, a temporary object is generated through the constructor. When the function returns, the temporary object is automatically destroyed. Such as the following example:

/ / count the number of occurrences of the character ch in the string str int countChar (const string& str, char ch); char buffer []; char c; / / call the above function countChar (buffer, c)

The first parameter we look at is char [], while the parameter type of the function is const string&, to see if it can be implicitly converted. The string class has a constructor that can be used as an implicit conversion function (see 5). Then the compiler generates a temporary variable of string, which is constructed with buffer as an argument, and the str parameter in countChar is bound to this temporary variable until the function returns.

Note that such a conversion occurs only in two cases: the function parameter is passed as a by value or the object is passed to a reference-to-const parameter.

Value transfer method:

Int countChar (string str, char ch); string buffer; char c; / / parameter passes countChar (buffer, c) by value

This method calls the copy constructor of string to generate a temporary variable, binds the temporary variable to str, and destroys it when the function returns.

Pass constant reference:

The initial instance is in this case immediately, but it must be emphasized that the reference is passed, such as changing the prototype of the start function to

Int countChar (string& str, char ch)

The following call is the same, and the compiler will report an error! Why is C++ designed to require that implicit type conversions do not occur when an object is passed to a reference-to-non-const parameter?

The following example may show you the purpose of this design:

/ / declare one that converts all characters in str to uppercase void toUpper (string& str); char buffer [] = "hazirguo"; toUpper (buffer); / / errorcodes! Non-const reference passing parameters cannot be implicitly converted

If the compiler allows the above pass to complete, a temporary object is generated, and the toUpper function converts the characters of the temporary variable to uppercase and returns a destroyed object, but has no effect on the buffer content! The purpose of the program is to expect to modify the "non-temporary object", but if the reference-to-non-cosnt object is converted, the function will only modify the temporary variable. This is why C++ forbids the non-const-reference parameter to generate temporary variables.

(2) when the function returns the object

When a function returns an object, the compiler generates a temporary object return, such as declaring a function to merge two strings:

Const string strMerge (const string S1, const string S2); most of the time, it is impossible to avoid the generation of such temporary variables, but modern compilers can optimize such temporary variables. In this optimization strategy, there is a so-called "return value optimization", which will be explained in the next article. Summary: temporary objects have construction and destructing costs that affect the efficiency of the program, so eliminate them as much as possible. What is more important is to quickly find out where temporary objects are generated:

When we see a reference-to-const parameter, it is very likely that a temporary object will be bound to it

When we see that a function returns an object, a temporary object is generated.

Reference: http://www.cnblogs.com/hazir/archive/2012/04/18/2456144.html

Return value Optimization (return value optimization) in C++

Return value optimization (Return Value Optimization, referred to as RVO) is such an optimization mechanism: when a function needs to return an object, if you create a temporary object user to return, then the temporary object consumes the cost of a call to a constructor (Constructor), a call to a copy constructor (Copy Constructor), and a call to a destructor (Destructor). If you do a little optimization, you can reduce the cost to the cost of a constructor. Here is a test done in Debug mode of Visual Studio 2008: (the compiler itself may have optimized RVO when testing under GCC, and you can't see the difference between the two kinds of code.)

/ / C++ Return Value Optimization

/ / author: code lunatic

/ / blog: http://www.programlife.net/

# include

Using namespace std

Class Rational

{

Public:

Rational (int numerator = 0, int denominator = 1): n (numerator), d (denominator) {

Cout

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