return-by-value

C++ Return value, reference, const reference

帅比萌擦擦* 提交于 2019-12-31 08:25:03
问题 Can you explain to me the difference between returning value, reference to value, and const reference to value? Value: Vector2D operator += (const Vector2D& vector) { this->x += vector.x; this->y += vector.y; return *this; } Not-const reference: Vector2D& operator += (const Vector2D& vector) { this->x += vector.x; this->y += vector.y; return *this; } Const reference: const Vector2D& operator += (const Vector2D& vector) { this->x += vector.x; this->y += vector.y; return *this; } What is the

C++ Return value, reference, const reference

一个人想着一个人 提交于 2019-12-31 08:24:49
问题 Can you explain to me the difference between returning value, reference to value, and const reference to value? Value: Vector2D operator += (const Vector2D& vector) { this->x += vector.x; this->y += vector.y; return *this; } Not-const reference: Vector2D& operator += (const Vector2D& vector) { this->x += vector.x; this->y += vector.y; return *this; } Const reference: const Vector2D& operator += (const Vector2D& vector) { this->x += vector.x; this->y += vector.y; return *this; } What is the

force const storing of returned by value value

£可爱£侵袭症+ 提交于 2019-12-24 00:18:41
问题 This is what I'm trying to accomplish: struct test{}; const test returnconst(){ return test(); } test returnnonconst(){ return test(); } int main(){ test t1=returnnonconst(); const test t2=returnnonconst(); test t3=returnconst(); //I want this to be a compile error const test t4=returnconst(); } The compiler accepts all of the four return* calls. I understand that in the third call a copy of the object is constructed, but I want instead to force the caller of returnconst to store the value as

Optimal way to return local value in C++11

人盡茶涼 提交于 2019-12-22 15:25:47
问题 In the old days, if I wanted a string representation of an object A , I would write something with the signature void to_string(const A& a, string& out) to avoid extra copies. Is this still the best practice in C++11, with move semantics and all? I have read several comments on other contexts that suggest relying on RVO and instead writing string to_string(const A& a) . But RVO is not guaranteed to happen! So, how can I, as the programmer of to_string, guarantee the string is not copied

Optimal way to return local value in C++11

流过昼夜 提交于 2019-12-22 15:25:14
问题 In the old days, if I wanted a string representation of an object A , I would write something with the signature void to_string(const A& a, string& out) to avoid extra copies. Is this still the best practice in C++11, with move semantics and all? I have read several comments on other contexts that suggest relying on RVO and instead writing string to_string(const A& a) . But RVO is not guaranteed to happen! So, how can I, as the programmer of to_string, guarantee the string is not copied

Why should return-by-value be const for non-builtin types but not const for builtin types?

空扰寡人 提交于 2019-12-22 08:37:30
问题 Solutions 4 and 5 on GotW #6 Const-Correctness mention this: Point GetPoint( const int i ) { return points_[i]; } Return-by-value should normally be const for non-builtin return types .. int GetNumPoints() { return points_.size(); } .. since the int is already an rvalue and to put in 'const' can interfere with template instantiation and is confusing, misleading, and probably fattening . I have the following questions Which template instantiation are we interfering with here ?! What exactly is

Why we use reference return in assignment operator overloading and not at plus-minus ops?

南楼画角 提交于 2019-12-03 07:24:48
问题 As I read in books and in the web, in C++ we can overload the "plus" or "minus" operators with these prototypes (as member functions of a class Money ): const Money operator +(const Money& m2) const; const Money operator -(const Money& m2) const; and for the assignment operator with: const Money& operator =(const Money& m2); Why use a reference to a Money object as a return value in the assignment operator overloading and not in the plus and minus operators? 回答1: Returning a reference from

Returning Vectors standard in C++

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 22:22:35
问题 Now, I know this is a common question, but I haven't been able to really find a straight answer on this. This is really a question about standards. I am working on a project involving the genetic algorithm. But I'm running into a bottleneck when it comes to returning a vector. Is there a "proper" way to do this. Normally I use dynamically allocated arrays, and return a pointer to a newly created array. obj* func(obj* foo); That way, everything is efficient and there is no copying of data. Is

Why we use reference return in assignment operator overloading and not at plus-minus ops?

旧街凉风 提交于 2019-12-02 20:54:38
As I read in books and in the web, in C++ we can overload the "plus" or "minus" operators with these prototypes (as member functions of a class Money ): const Money operator +(const Money& m2) const; const Money operator -(const Money& m2) const; and for the assignment operator with: const Money& operator =(const Money& m2); Why use a reference to a Money object as a return value in the assignment operator overloading and not in the plus and minus operators? Returning a reference from assignment allows chaining: a = b = c; // shorter than the equivalent "b = c; a = b;" (This would also work

C++ Return value, reference, const reference

浪子不回头ぞ 提交于 2019-12-02 15:30:34
Can you explain to me the difference between returning value, reference to value, and const reference to value? Value: Vector2D operator += (const Vector2D& vector) { this->x += vector.x; this->y += vector.y; return *this; } Not-const reference: Vector2D& operator += (const Vector2D& vector) { this->x += vector.x; this->y += vector.y; return *this; } Const reference: const Vector2D& operator += (const Vector2D& vector) { this->x += vector.x; this->y += vector.y; return *this; } What is the benefit of this? I understand the sense behind const reference passing to function as you want to make