Why the copy constructor didn't work when the function returned?

62 Views Asked by At

I'm new to C++, I'm confused of the copy constructor.

When I run my code below, it only shows me constructor and destructor without copy-constructor.

But when the function foo_bar() returns a object point, shouldn't a copy of point be created and returned?

I don't understand, or maybe there are some misunderstandings.

Here is my code.

#include <iostream>

using namespace std;

class Point {
  public:
    Point() { cout << "constructor Point()" << endl; }
    Point(const Point &point) {
        cout << "copy constructor Point(const Point&)" << endl;
    }
    ~Point() { cout << "destructor ~Point()" << endl; }
};

Point foo_bar() {
    Point point;
    return point; // copy constructor???
}

int main() {
    Point A;
    foo_bar();
    return 0;
}

Here is the result of my code.

constructor Point()
constructor Point()
destructor ~Point()
destructor ~Point()

I think there should be a copy-constructor.

0

There are 0 best solutions below