0

What is the difference between creating class object in the two following ways:

class cat 
{
  private: 
     int age; 
  public: 
     cat(); 
}; 


int main(void) 
{
  cat object; // static object 
  cat *pointer = new cat(); // dynamic object 
}
Qantas 94 Heavy
  • 14,790
  • 31
  • 61
  • 78
Greg
  • 11
  • 1
  • 1
  • The keywords here are heap and stack: https://stackoverflow.com/questions/5836309/stack-memory-vs-heap-memory https://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap – Lanting Nov 06 '14 at 09:45
  • The first one isn't static, it's automatic. – molbdnilo Nov 06 '14 at 10:02

1 Answers1

2

The first one is declaring a static variable (usually on the stack*) that will die at the end of the code block in which it is defined.

The second one is dynamically allocating a variable (usually on the heap*) which means that you are the one that can decide where to deallocate it with delete[] (and yes you should remember to do it).

Anna
  • 107
  • 1
  • 9