简体   繁体   English

通过指针访问成员函数

[英]Accessing member functions through pointers

Why am I getting the address in the output.为什么我在输出中得到地址。 Rather I should get the Output Length= (value input by user) , Width = (value input by user).相反,我应该得到 Output Length= (value input by user) , Width = (value input by user) 。 As in the main body of program after getting input R1.getdata() , ptr->result() should display the result of Rectangle class.正如在获取输入R1.getdata()后的程序主体中, ptr->result()应该显示 Rectangle 类的结果。

#include <iostream>

using namespace std;

class Rectangle {
   protected:
    float length;
    float width;

   public:
    void getdata() {
        cout << "Enter length and width= ";
        cin >> length >> width;
    }
    void result() {
        cout << "Length = " << length << "\nWidth = " << width << endl;
    }
};
class Area : public Rectangle {
   private:
    float area;

   public:
    void calc_area() { area = length * width; }
    void result() { cout << "Area = " << area << endl; }
};
class Perimeter : public Rectangle {
   private:
    float perimeter;

   public:
    void calc_peri() { perimeter = 2 * (length + width); }
    void result() { cout << "Perimeter = " << perimeter << endl; }
};
void main() {
    Rectangle R1;
    Area A1;
    Perimeter P1;
    Rectangle *ptr;
    R1.getdata();
    ptr = &A1;
    ptr->result();
}

You are getting the wrong values, because you are calling ptr->result();你得到了错误的值,因为你正在调用ptr->result(); on a uninitialized Area object (A1), which has been upcasted from pointer to Rectangle object.在未初始化的区域对象 (A1) 上,该对象已从指向 Rectangle 对象的指针向上转换。

The values the user inputs though are used in the R1 object, which you then don't use anymore.用户输入的值在R1对象中使用,然后您不再使用。 Moreover, you should make the result() method virtual.此外,您应该将result()方法设为虚拟。

Lastly, the syntax for calling base class method on a pointer to an inheriting class is : ptr->Rectangle::result();最后,在指向继承类的指针上调用基类方法语法是ptr->Rectangle::result(); . .

Below you will find your code with some fixes that demonstrate things I wrote about:您将在下面找到带有一些修复的代码,这些修复演示了我所写的内容:

#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class Rectangle {
   protected:
    float length;
    float width;

   public:
    void getdata() {
        cout << "Enter length and width= ";
        cin >> length >> width;
        std::cout << length << "  " << width << std::endl;
    }
    virtual void result() {
        cout << "(Rectangle) Length = " << length << "\nWidth = " << width
             << endl;
    }
};
class Area : public Rectangle {
   private:
    float area;

   public:
    void calc_area() { area = length * width; }
    void result() { cout << "Area = " << area << endl; }
};
class Perimeter : public Rectangle {
   private:
    float perimeter;

   public:
    void calc_peri() { perimeter = 2 * (length + width); }
    void result() { cout << "Perimeter = " << perimeter << endl; }
};
int main() {
    Rectangle R1;
    Area* A1;
    Perimeter P1;
    Rectangle* ptr;
    R1.getdata();
    ptr = &R1;
    A1 = static_cast<Area*>(ptr);
    // or:
    // A1 = (Area*)ptr;
    ptr->Rectangle::result();
}

Ptr 指向 Rectangle 类(Area 类)的一个子类的地址,因此它调用它所引用的对象(A1 类型的 A1)的成员(结果)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM