简体   繁体   中英

Why do we need a pointer to a class if we can instantiate an object of it and access members of the class?


I am a little confused here. If we can instantiate an object of a class and access member methods then why a pointer to a class? Is there any benefit to it? And when do we use a pointer to a class and when do we instantiate an object of it?

Thank you.

You might not need a pointer to a class. If your class is small, doesn't exhibit polymorphic behavior through a base class, or cost anything to instantiate, you can probably just rip one off on-the-fly and be done.

But in many cases, we need pointers because:

  1. The class is "big," as in, is expensive to instantiate or make copies of. In that case, we can pass around pointers instead of just creating them as-needed in order to be more efficient with RAM and CPU.
  2. You may not want there to be more than once instance of a class. Consider a logger, which is often implemented as a Singleton . Now Singletons are "Bad" according to many, but the use illustrates the point that sometimes instantiating a second copy of some class could break something. So you need a pointer (or reference) to the one-and-only copy.
  3. When you want run-time polymorphic behavior, you almost certainly want a pointer (or, preferably, a reference).

If we can instantiate an object of a class and access member methods then why a pointer to a class?

If you have just a base class and no derived classes, then it is good to create the object on the stack rather than pointer to the class. In the latter, you should be calling delete on the pointer to return the resources acquired by new . (Also make sure that stack never overflows. If you need large number of array of instances, then instantiate using new is the only option.)

class foo
{
    int a ;
};

foo obj1 ;
foo obj2 = obj1 ; // Copy construction. Both obj2, obj1 have it's independent
                  // copy of objects. (Deep copy)

foo *obj3 = new foo ;
foo *obj4 = obj3 ; // Copy construction. However, both obj3, obj4 point to the 
                   // same object. (Shallow copy)

                   // You should be careful in this case because deletion of obj3
                   // makes obj4 dangling and vice versa.

Is there any benefit to it?

Polymorphism because it works only for pointers / references.

And when do we use a pointer to a class and when do we instantiate an object of it?

It depends on the requirement. As said earlier, if you have just a base class creation of object on stack is a better option.

One reason might be performance. Imagine you have 100 instances of some class. If you are not using pointers and you want to do something like copy those instances from one container to another, there is quite a bit of overhead as the copy constructor would need to be called on each one. However, if you had pointers to those instances instead then the only thing really being copied is the pointer making the operation much much quicker.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM