调用成员函数

Call member function

本文关键字:函数 成员 调用      更新时间:2023-10-16

我不知道如何从main调用类成员函数。我想用一个poly对象作为它的隐式参数来调用"printPoly"。

这是类的定义:

class poly
{
private:
    Node *start;    
public:
    poly(Node *head)  /*constructor function*/
    {
        start = head;
    }
    void printPoly(); //->Poly *polyObj would be the implicit parameter??
};
void poly :: printPoly()
{
    //....
}

这是呼叫代码:

poly *polyObj;
polyObj = processPoly(polynomial); // processPoly is a function that returns a poly *
polyObj.printPoly(); // THIS IS WHERE THE PROBLEM IS

现在我想用我刚刚创建的polyObj调用"printPoly"。

必须先取消引用指针,然后才能调用函数。指针包含对象的地址,取消引用将返回对象。因此:

(*polyObj).printPoly();

然而,这通常被缩短为:

polyObj->printPoly();