3

Does C++ compiler use the default destructor when instances created on a stack are out of their scope? I am a bit confused because I assumed destructor is called when memory is allocated on a heap. I used the code example below.

enum Stage{starter, inbetween, graduating};
class Student
{
public:
   Student(const std::string & a_name, Stage a_stage);
private:
const std::string name;
}; 

and in the main

int main( void )
{
    Student s1{"Konjit", graduate}; // student instance on stack
    return 0;
}
Konjit W
  • 216
  • 1
  • 6
  • 6
    That's the consequence of all those bad tutorials and teachers creating object only through the usage of `new`... +1 for asking – YSC Feb 05 '19 at 13:50
  • 1
    I agree this should be a duplicate of something, though the answer in the duplicate doesn't include stack objects, only allocated ones – JVApen Feb 05 '19 at 19:14
  • 1
    A better read: https://stackoverflow.com/a/6403057/2466431 – JVApen Feb 05 '19 at 19:15

0 Answers0