【问题标题】:How to understand the dynamic binding in this C++ code?如何理解此 C++ 代码中的动态绑定?
【发布时间】:2020-06-09 05:49:17
【问题描述】:

这是我的代码。我需要帮助使这个动态绑定工作。

#include<iostream>
#include<conio.h>
using namespace std;

class Shape {
    protected:
    double x,y;
    public:
    void get_data(double a,double b=3.14)   {
        x=a;
        y=b;
    }   
    virtual void display_area(){
    };
};

class Rectangle : public Shape  {
    public:
    void display_area() {
        cout<<"\nArea of Rectangle : "<<x*y<<" units.\n";
    }
};
class Circle : public Shape {
    public:
    void display_area() {
        cout<<"\nArea of Circle : "<<y*x*x<<" units.\n";
    }
};
class Triangle : public Shape   {
    public:
    void display_area() {
        cout<<"\nArea of Triangle : "<<0.5*x*y<<" units.\n";
    }
};

main()  {
    Shape *ptr;
    char opt,wait;
    double a,b;
    do{
        system("cls");
        cout<<"\n1.Area of Rectangle\n2.Area of Circle\n3.Area of Triangle\n4.Exit\n\nEnter your Option : ";
        opt=getche();
        switch(opt) {
            case '1':
                *ptr = new Rectangle;
                cout<<"\nEnter Length : ";
                cin>>a;
                cout<<"\nEnter Width  : ";
                cin>>b;
                ptr->get_data(a,b);
                ptr->display_area();
                break;
            case '2':
                *ptr = new Circle;
                cout<<"\nEnter Radius : ";
                cin>>a;
                ptr->get_data(a);
                ptr->display_area();
                break;
            case '3':
                *ptr = new Triangle;
                cout<<"\nEnter Base   : ";
                cin>>a;
                cout<<"\nEnter Height : ";
                cin>>b;
                ptr->get_data(a,b);
                ptr->display_area();
                break;
            case '4':
                cout<<"\n\nGoodBye ! Have a Nice Day !\n";
                exit(0);
            default:
                cout<<"\n\nEnter a Valid Option !\n";
        }
        cout<<"\n\nPress Any Key to Continue....";
        wait=getche();
    }while(opt!='4');
}

我得到的错误是:

5   7  [Note] Shape& Shape::operator=(const Shape&)

5   7  [Note] no known conversion for argument 1 from 'Rectangle*' to 'const Shape&'

55  10 [Error] no match for 'operator=' (operand types are 'Shape' and 'Circle*')

55  10 [Note] candidate is:

5   7  [Note] Shape& Shape::operator=(const Shape&)

5   7  [Note] no known conversion for argument 1 from 'Circle*' to 'const Shape&'

62  10 [Error] no match for 'operator=' (operand types are 'Shape' and 'Triangle*')

62  10 [Note] candidate is:

5   7  [Note] Shape& Shape::operator=(const Shape&)

5   7  [Note] no known conversion for argument 1 from 'Triangle*' to 'const Shape&'

【问题讨论】:

标签: c++ virtual-functions dynamic-binding


【解决方案1】:

您错误地分配了ptr。将对象分配给它时,您不应该取消引用它。

改变这个

*ptr = ...;

到这里

ptr = ...;

此外,您正在泄漏使用new 分配的对象。使用完每个对象后,您需要致电 delete ptr;。这也意味着您需要向Shape 添加一个虚拟析构函数,以便正确调用正确的后代析构函数:

class Shape {
... 
public:
    virtual ~Shape() {}
...
};

【讨论】:

  • 这真的很有帮助。它解决了我的问题。请告诉我更多关于“取消引用”的信息。我很困惑。
  • @Cyber​​-x-x-Stunner 你应该阅读good C++ book。关于指针是什么以及它们如何工作的整章都有。
【解决方案2】:

我想补充一下。不要使用原始指针。 如果您使用智能 ponter std::unique_ptr,那么您可以避免错误。而且您无需考虑删除对象。 例如:

std::unique_ptr<Shape> ptr;
ptr = std::make_unique<Rectangle>();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-28
    • 2020-08-18
    • 1970-01-01
    • 2014-03-21
    • 1970-01-01
    • 2016-07-23
    • 2021-09-12
    相关资源
    最近更新 更多