In C++ Suppose I have
class Sample{
public:
void someFunction();
};
In main() is there any difference between doing
Sample obj;
obj.someFunction();
AND
Sample *obj = new Sample();
obj->someFunction();
Is it only a matter of syntax or is there a performance/implementation difference? When should one be used over the other?
This is simple stuff - to do with heaps and stacks
Sample obj;
obj.someFunction();
obj is on the stack
AND
Sample *obj = new Sample();
obj->someFunction();
Is on the heap.
This needs to be deleted. It also lives outside scope.
The performance is about the same
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With