Skip to content
Sign upLog in
← Back to Community
What is the difference between declaring class as normal vs declaring it as a pointer?
Profile icon
Qaimaq

Are there any difference between them? I tried to test it out, but they are giving me the identical results so what is the pro and cons of one another?

CCow *cow = new CCow();

vs

CCow cow;

Voters
Profile icon
Sister2
Profile icon
Qaimaq
Comments
hotnewtop
Profile icon
19wintersp

A pointer is a pointer towards a region of memory where the data is stored. A "regular" one just holds the data in itself. The difference in your case is where they are stored: the "regular" one is stored on the stack, and the pointer one is stored on the heap. If you were to pass one of these instances to a function, for example, and the function attempted to modify them, the "regular" one would become a copy within the function (cloning by value) because the literal data is being passed, whereas the pointer one would remain a pointer to the original (cloning by reference) because the location of the literal data is being passed. Objects allocated with new must also be deallocated with delete because they are not cleared when the function exits.