0

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? Can I implement linked list using obj instead of *obj?

  • Missing `delete obj;` for the later. – Jarod42 Jun 08 '21 at 07:49
  • You don't have a "pointer of the class", you have a "pointer to an *object*". – Some programmer dude Jun 08 '21 at 07:50
  • And yes there is a difference. The main difference (besides the different type of the variables `obj`) being where the object is being located. Another difference is that for the pointer you need to use *indirection* to reach the object itself from the pointer (by dereferencing the pointer). – Some programmer dude Jun 08 '21 at 07:52
  • In more concrete terms, it is the difference between *what* something is, and *where* something is. – molbdnilo Jun 08 '21 at 08:25

0 Answers0