呼叫会员功能

时间:2014-04-20 16:12:57

标签: c++ class object member-function-pointers

我不知道如何从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”。

1 个答案:

答案 0 :(得分:4)

在调用函数之前,必须取消引用指针。指针包含对象的地址,解除引用返回对象。所以:

(*polyObj).printPoly();

但是,这通常缩短为:

polyObj->printPoly();
相关问题