Home Why should I use a pointer rather than the object itself?
Reply: 0

Why should I use a pointer rather than the object itself?

user750
1#
user750 Published in June 24, 2018, 3:08 am

I'm coming from a Java background and have started working with objects in C++. But one thing that occurred to me is that people often use pointers to objects rather than the objects themselves, for example this declaration:

Object *myObject = new Object;

rather than:

Object myObject;

Or instead of using a function, let's say testFunc(), like this:

myObject.testFunc();

we have to write:

myObject->testFunc();

But I can't figure out why should we do it this way. I would assume it has to do with efficiency and speed since we get direct access to the memory address. Am I right?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.450768 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO