c++ Copy Substitution Operator Question

Asked 1 weeks ago, Updated 1 weeks ago, 1 views

Test&operator=(const Test&t)
    {
        cout << "Test& operator=" << endl;
        return *this;
    }

Test operator=(const Test& t)
    {
        cout << "Test operator=" << endl;
        return *this;
    }

While studying the copy substitution operator

Both of the chords above work I'm curious about the difference between Test&operator= and Testoperator=

c++

2022-09-20 10:55

1 Answers

If the return value of a function is in the form of a generic datatype (Test), it returns the value by creating a new, unnamed temporary variable on the side that called the function before returning the return value.

On the other hand, when the return value is in reference format (Test&), it returns the variable specified after the return that was accessible inside the function to the location where the function was called in reference format without creating a new temporary variable. Care must be taken to avoid returning the local variable of the function.

The question asked about Test and Test& in the return format, but this is not special, for example, if the return format is int and int&.


2022-09-20 10:55

If you have any answers or tips


© 2022 pinfo. All rights reserved.