Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ object, difference between dynamic and static [duplicate]

Tags:

c++

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 
}
like image 820
Greg Avatar asked Sep 19 '25 12:09

Greg


1 Answers

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).

like image 196
Anna Avatar answered Sep 21 '25 04:09

Anna