Call member function

孤者浪人 提交于 2019-12-11 08:32:26

问题


I don't know how to call a class member function from main. I want to call "printPoly" with a poly object as its implicit parameter.

Here is the class definition:

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()
{
    //....
}

Here is the calling code:

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

Now I want to call "printPoly" with the polyObj I just created.


回答1:


You must dereference the pointer before you can call the function. The pointer contains the address of the object, dereferencing returns the object. So:

(*polyObj).printPoly();

However, this is usually shortened to:

polyObj->printPoly();


来源:https://stackoverflow.com/questions/23184662/call-member-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!