How to call a method via a vector?

前端 未结 2 1722
日久生厌
日久生厌 2020-12-21 22:12

How do I call a method of an object which is stored within a vector? The following code fails...

    ClassA* class_derived_a = new ClassDerivedA;
    ClassA*         


        
2条回答
  •  感动是毒
    2020-12-21 22:44

    The things in the vector are pointers. You need:

    (*it)->printOutput();
    

    which dereferences the iterator to get the pointer from the vector, then uses -> on the pointer to call the function. The syntax you show in your question would work if the vector contained objects rather than pointers, in which case the iterator acts like a pointer to one of those objects.

提交回复
热议问题