调用成员函数 [英] Call member function

查看:113
本文介绍了调用成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何从main调用类成员函数。



这里是类的定义:

  class poly 
{
private:
Node * start;
public:
poly(Node * head)/ *构造函数* /
{
start = head;
}

void printPoly(); // - > Poly * polyObj将是隐式参数??
};

void poly :: printPoly()
{
// ...
}

这里是调用代码:

  poly * polyObj; 
polyObj = processPoly(polyomial); // processPoly是返回poly *
的函数polyObj.printPoly(); //这是问题在哪里是

现在我想使用polyObj调用printPoly


解决方案

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

 (* polyObj).printPoly(); 

但是,这通常缩写为:

  polyObj-> printPoly(); 


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.

解决方案

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();

这篇关于调用成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆