简体   繁体   English

如果我们可以实例化一个对象并访问该类的成员,为什么我们需要一个指向类的指针?

[英]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. 在这种情况下,我们可以传递指针而不是仅仅根据需要创建指针,以便更有效地使用RAM和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 . 考虑一个记录器,它通常被实现为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 . 在后者中,您应该在指针上调用delete来返回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.) (还要确保堆栈永远不会溢出。如果需要大量的实例数组,那么使用new实例化是唯一的选择。)

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. 想象一下,你有一些类的100个实例。 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. 但是,如果你有指向那些实例的指针,那么唯一真正被复制的是指针使操作更快。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过基类的指针,我们无法访问派生类特定成员 - Through base class's pointer we can't access derived class specific members 为什么我们可以使用指针访问私有数据成员class,而不使用class中的其他成员朋友function? - Why can we access private data members class using pointers, without using members friend function other members in the class? 自定义异常类-我们为什么需要它? - Custom Exception Class - Why do we need it? 为什么我们必须在对象地址前面提到数据类型才能访问C ++中类的私有成员? - Why we have to mention data type in front of address of the object to access the private members of a class in C++? 为什么我们不能在他们的声明中初始化班级成员? - Why can't we initialize class members at their declaration? 为什么我们可以在班级范围之外定义私人成员 - Why can we define private members outside of their class scope 为什么我们需要使用std :: vector :: pointer来访问元素? - Why do we need to use std::vector::pointer to access elements? 为什么我们需要超类的指针指向子类的对象? - Why do we need a pointer of superclass to point to a object of a subclass? 我们能否存储 class object 而不仅仅是指向该 object 的指针 - Can we store the class object and not just the pointer to that object 为什么在创建指针类型对象时内存没有分配给类? - Why memory is not allocated to class when we create pointer type object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM