0

I am confused because I found some information about lifetime of object in C++:

https://en.cppreference.com/w/cpp/language/lifetime

For any object of class types whose destructor is not trivial, lifetime ends when the execution of the destructor begins.

  1. Why, is there any rationale?

  2. If that means I shouldn't do this?

    #include <iostream>
    
    class Foo {
    public:
     Foo() = default;
     Foo(const Foo&) = default;
     ~Foo() { std::cout << member; }
    
     int member = 666;
    };
    
    int main() {
      Foo a;
    }
    
BartekPL
  • 2,290
  • 1
  • 17
  • 34
  • Ok, could you answer to me? I didn't find answer to my questions in link you added. – BartekPL Jun 27 '18 at 10:49
  • 2
    The argument is symmetry. The lifetime of an object begins when construction is finished. Since destruction is logically a reverse sequence of construction, this means lifetime ends when the destructor starts. Your example is valid - the lifetime of members begins before execution of the constructor body, and ends after execution of the destructor. – Peter Jun 27 '18 at 10:50
  • Thanks @Peter ;) – BartekPL Jun 27 '18 at 10:52

0 Answers0