Rich Gg
Rich Gg

Reputation: 53

Order of execution in c++ program

Can someone help me to understand why the following demo program , compiled with a gnu compiler, gives the output specified below

#include <iostream>

class Obj {
   public:
    Obj() { std::cout << "Obj Constructor\n"; }
    ~Obj() { std::cout << "Obj Destructor\n"; }
};

double eval(const Obj fun) {
    std::cout << "eval fun" << std::endl;
    return 1.0;
}
int main() {
    Obj obj;
    double res = eval(obj) - eval(obj);
    std::cout << res << '\n';
    return 0;
}
Obj Constructor
eval fun
eval fun
Obj Destructor
Obj Destructor
0
Obj Destructor

Why the Destructor is not called right a way when one of the eval function is called first time.

Upvotes: 3

Views: 116

Answers (0)

Related Questions