Several cases about function return value

Source: Internet
Author: User

in the interior of a function, return returns a copy, whether the variable, object, or pointer is a copy, but the copy is a shallow copy.

1. If a variable of a basic type is returned, for example:

int a;a = 5;return A

  then a5 return, then aa was destroyed, but its copy 5 or successfully returned, so there's no problem with that.

2. But for non-dynamic allocation (NEW/MALLOC) pointers, like 1, There are problems, such as inside a function:

int a[] = {1, 2};return A;

Then the pointer is also returnedAa0x002345fc0x2345fc is able to return successfully. When return After the completion of the execution, a0x002345fc0x002345fc

3. Another case of returning ( dynamically assigned ) pointers, such as inside a function:

int a = new int (5); return A;

It is possible to do so. when return a is executed,a is not destroyed (must be deleted to destroy a), so the A returned here is valid.

4. If it is not a basic data type, such as:

Class A{public:               Otherclass * ...};

If there is a class A local variable inside a function , such as:

A A;return A;

A copy of A is also returned , and if a does not have a deep copy constructor, it will call the default copy constructor ( shallow copy ), which will fail;

This is possible if a deep copy constructor is provided in a.

The experiment code is as follows:

#include <iostream>using namespace Std;int some_fun1 () {int a = 5;return A;                   ok}int* some_fun2 () {int a = 5;int *b = &a;return b;                   Not ok}int* some_fun3 () {int *c = new int (5); return C; OK, return C is not destroyed after execution (must use Delete to destroy)}class csomething{public:int a;int b;public:csomething (int a, int b) {this-> A = A; This->b = b;}};            Class ca{private:csomething* sth; Member Variable PUBLIC:CA (csomething* sth) {this->sth = new csomething (Sth->a, sth->b) in the form of pointers;} If you do not implement a deep copy, note this copy constructor CA (ca& obj) {sth = new csomething ((Obj.sth)->a, (obj.sth)->b);} ~ca () {cout << "in the destructor of class CA ..." << endl;if (NULL! = sth) delete sth;} void Show () {cout << ("<< sth->a <<", "<< sth->b <<") "<< Endl;} void SetValue (int a, int b) {sth->a = A;sth->b = b;} void Getsthaddress () {cout << sth << Endl;}}; CA some_fun4 () {csomething C (1, 2); CA a (&c); RetuRN A;              If the CA does not implement a deep copy, then not OK, if deep copy is implemented, then ok}int main (int argc, char* argv[]) {int a = SOME_FUN1 (); cout << a << Endl;             Okint *b = some_fun2 () cout << *b << Endl;           Not OK, even if the return result is correct, it is just luck good int *c = SOME_FUN3 ();             OK, when return C is executed, C is not destroyed (you must use Delete to destroy) cout << *c << endl;delete c;ca d = some_fun4 (); Not OK If the CA does not implement a deep copy, or okd.show () If deep copy is implemented; return 0;}

Several cases about function return value

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.