Return values in C + +

Source: Internet
Author: User

There are roughly three return values in C + +: value copies (replicas), value references, and pointers, and what type of values to return depends on the situation.

If you return a copy of a large object, it is a time-consuming process to return after each function call and call the copy constructor of the object type to construct a new copy.

Therefore, when you do not need to return the object copy, try to return the object's reference or pointer, at the same time, if you do not change the reference or the object pointing to the contents of the objects, plus the const qualifier.

Whether it's a return copy, a reference, or a pointer, the return value is used as an lvalue, so you can also assign the return value to another variable (the type can be a value type, a reference type, or a pointer type)

Instance:

Class Widget {Public:widget () {a = 0;printf ("widget constructor with default value:this:%p a:%d \ n", this, a);} Widget (int v) {a = v;printf ("widget constructor with parameters:this:%p a:%d \ n", this, a);} Widget (const widget& widget) {this->a = widget.a;printf ("Widget copy constructor with reference:this:%p widget:%p a:%d \ n ", this, &widget, a);} Widget (const widget* widget) {this->a = widget->a;printf ("Widget copy constructor with pointer:this:%p widget:%p A: %d \ n ", this, widget, a);}  widget& operator = (const widget& widget) {this->a = widget.a;printf ("widget operator =: this:%p widget:%p a:%d \ n ", this, &widget, a); return *this;}  widget& operator + (const widget& widget) {this->a + = widget.a;printf ("widget operator +: this:%p widget:%p a:%d \ n ", this, &widget, a); return *this;} Widget Getbycopy () {printf ("Widget Get object by copy:this:%p a:%d \ n", this, a); return *this;} widget& Getbyref () {printf ("Widget get object by ref:this:%p a:%d \N ", this, a); return *this;} widget* Getbypointer () {printf ("Widget get object by pointer:this:%p a:%d \ n", this, a); int A;};
int _tmain (int argc, _tchar* argv[]) {widget W1 (1);//Call constructor widget (int v) widget W2 (2); Widget W3; Call the default constructor W3 = (W1 + w2); Call the + operator first, in the call = operator printf ("\ n");p rintf ("W4--------------------\ n"); w3.getbycopy (); Returns the copy of printf ("W4_1--------------------\ n"); Widget w4_1 = W3.getbycopy (); The feeling should be two copies, actually only once the widget (const widget& widget) copy constructor call, estimate is compiler optimization printf ("W4_2--------------------\ n"); Widget w4_2;w4_2 = W3.getbycopy (); Call the widget (const widget& widget) copy constructor, then call = Assign copy function printf ("W4_3--------------------\ n"); widget& W4_3 = W3.getbycopy (); Call the widget (const widget& widget) copy constructor printf ("--------------------\ n \ nthe");p rintf ("W5--------------------\ n" ); W3.getbyref (); Returns a reference to printf ("W5_1--------------------\ n"); Widget W5_1 = W3.getbyref (); Call the widget (const widget& widget) copy constructor printf ("W5_2--------------------\ n"); Widget w5_2;w5_2 = W3.getbyref (); There is no call to copy constructor, call once = assign copy function printf ("W5_3--------------------\ n"); widget& W5_3 = W3.getbyref (); Direct assignment to the reference printf ("--------------------\ n");p rintf ("W6--------------------\ n"); W3.getbypointer (); Returns the pointer printf ("W6_1--------------------\ n"); Widget W6_1 = W3.getbypointer (); Call the widget (const widget* widget) copy constructor printf ("W6_2--------------------\ n"); Widget w6_2;w6_2 = W3.getbypointer (); Since there is no assignment copy function for pointers, the copy constructor is called first, a temporary variable is created, and the = Assignment copy function printf ("W6_3--------------------\ n") is called; widget* w6_3 = W3.getbypointer (); Direct assignment to Pointer printf ("--------------------\ n"); GetChar (); return 0;}

Run Result:

Widget constructor with Parameters:this:0035fb70 A:1widget constructor with parameters:this:0035fb64 A:2widget Construc Tor with default value:this:0035fb58 a:0widget operator +: this:0035fb70 widget:0035fb64 a:3widget operator =: this:0035f B58 widget:0035fb70 a:3w4--------------------widget Get object by copy:this:0035fb58 A:3widget copy constructor with ref  ERENCE:THIS:0035F9FC widget:0035fb58 a:3w4_1--------------------widget Get object by copy:this:0035fb58 a:3widget copy Constructor with reference:this:0035fb4c widget:0035fb58 A:3w4_2--------------------Widgets constructor with default VA LUE:THIS:0035FB40 A:0widget Get object by copy:this:0035fb58 A:3widget copy constructor with reference:this:0035fa08 WI dget:0035fb58 a:3widget operator =: this:0035fb40 widget:0035fa08 a:3w4_3--------------------widget Get object by copy:t his:0035fb58 A:3widget copy constructor with reference:this:0035fb28 widget:0035fb58 a:3--------------------W5------- -------------Widget Get Object by ref:this:0035fb58 A:3w5_1--------------------Widgets Get object by ref:this:0035fb58 A:3widget copy constructor with reference:this:0035fb1c widget:0035fb58 A:3w5_2--------------------widget constructor with default value:this:0 035FB10 A:0widget Get object by ref:this:0035fb58 a:3widget operator =: this:0035fb10 widget:0035fb58 a:3w5_3----------- ---------Widget Get object by ref:this:0035fb58 a:3--------------------W6--------------------Widgets Get object by point er:this:0035fb58 a:3w6_1--------------------Widget Get object by pointer:this:0035fb58 A:3widget copy constructor with Pointer:this:0035faf8 widget:0035fb58 A:3w6_2--------------------widget constructor with default VALUE:THIS:0035FAEC A:0widget Get object by pointer:this:0035fb58 A:3widget copy constructor with Pointer:this:0035fa14 widget:0035fb58 A:3W Idget operator =: this:0035faec widget:0035fa14 a:3w6_3--------------------widget Get object by pointer:this:0035fb58 a: 3--------------------

  

Return values in C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.