If I have a class named Object, what’s the difference between creating an instance just like that:
Object var;
and:
Object* var = new Object();
?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Here you are creating var on the stack:
So in the above,
varis the actual object.Here you are creating var on the heap (also called dynamic allocation):
When creating an object on the heap you must call
deleteon it when you’re done using it. Alsovaris actually a pointer which holds the memory address of an object of typeObject. At the memory address exists the actual object.For more information: See my answer here on what and where are the stack and heap.