I saw this question When is an object "out of scope"?
I have taken a look at the sparc_spread's answer and I found one problem in it. In this section of his answer:
Circle myFunc () {
Circle c (20);
return c;
}
// The original c went out of scope.
// But, the object was copied back to another
// scope (the previous stack frame) as a return value.
// No destructor was called.
He has said that " No destructor was called. " But when I try to run this code ( Which was written by me):
/* Line number 1 */ #include <iostream>
/* Line number 2 */ #include <string>
/* Line number 3 */ using namespace std;
/* Line number 4 */ class test {
/* Line number 5 */ public:
/* Line number 6 */ test(int p) {
/* Line number 7 */ cout << "The constructor ( test(int p) ) was called"<<endl;
/* Line number 8 */ }
/* Line number 9 */ test(test&&c)noexcept {
/* Line number 10 */ cout << "The constructor ( test(test && c) ) was called" << endl;
/* Line number 11 */ }
/* Line number 12 */ ~test() {
/* Line number 13 */ cout << "The distructor was called" << endl;
/* Line number 14 */ }
/* Line number 15 */ };
/* Line number 16 */ test function() {
/* Line number 17 */ test i(8);
/* Line number 18 */ return i;
/* Line number 19 */ }
/* Line number 20 */ int main()
/* Line number 21 */ {
/* Line number 22 */ test o=function();
/* Line number 23 */ return 0;
/* Line number 24 */ }
The Output:
The constructor ( test(int p) ) was called
The constructor ( test(test && c) ) was called
The distructor was called
The distructor was called
So the output of my code shows that:
Two constructors were called ( and this is not the point I want to discuss. So I will not discuss ( Why, When or How) are two constructors called?)
Two destructors were called
And when I use the debugger (to know when the first destructor was called) I found that The first destructor is called in line number 18 (the line number 18 in my code).
And in the end. Is my point of view right?