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