问题
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