本文介绍了派生类的虚拟赋值运算符未被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 C++ 还很陌生,正在尝试处理虚拟赋值.下面的程序由一个具有两个数据成员的抽象基类和一个具有一个数据成员的派生类组成.当我设置一个指向派生对象的抽象指针时,程序使用 operator= 的抽象版本而不是派生版本,即使它们都被声明为虚拟".我在这里做错了什么?

I'm pretty new to C++, and am trying to come to grips with virtual assignment. The program below consists of an abstract base class with two data members, and a derived class with one. When I set an abstract pointer to a derived object, the program uses the abstract version of operator= rather than the derived version, even though they're both declared "virtual." What am I doing wrong here?

提前致谢,

#include <iostream>
#include <cstring>

class Abstract
{
  protected:
        char * label;
        int rating;
    public:
        Abstract(const char * l = "null", int r = 0);
        virtual Abstract & operator=(const Abstract & rs);
        virtual ~Abstract() { delete [] label; }
        virtual void view() const = 0;

};

class Derived : public Abstract
{
    private:
        char * style;
    public:
        Derived(const char * s = "none", const char * l = "null",
                  int r = 0);
        ~Derived() { delete [] style; }
        virtual Derived & operator=(const Derived & rs);
        virtual void view() const;

};

Abstract::Abstract(const char * l , int r )
{
    label = new char[std::strlen(l) + 1];
    std::strcpy(label, l);
    rating = r;
}

Abstract & Abstract::operator=(const Abstract & rs)
{
    if (this == &rs)
            return *this;
    delete [] label;
    label = new char[std::strlen(rs.label) + 1];
    std::strcpy(label, rs.label);
    rating = rs.rating;
    return *this;
}

Derived::Derived(const char * s, const char * l, int r)
         : Abstract(l, r)
{
    style = new char[std::strlen(s) + 1];
    std::strcpy(style, s);
}

Derived & Derived::operator=(const Derived & hs)
{
    if (this == &hs)
        return *this;
    Abstract::operator=(hs);
    style = new char[std::strlen(hs.style) + 1];
    std::strcpy(style, hs.style);
    return *this;
}

void Derived::view() const
{
    std::cout << "label: " << label << "
rating: "
              << rating << "
style: " << style;
}

int main ()
{
    using namespace std;
    char label[20], style[20];
    int rating;


    cout << "label? ";
    cin >> label;
    cout << "rating? ";
    cin >> rating;
    cout <<"style? ";
    cin >> style;

    Derived a;
    Abstract * ptr = &a;
    Derived b(style, label, rating);
    *ptr = b;
    ptr->view();

    return 0;
}

推荐答案

C++ 不允许您使用协变参数类型覆盖虚函数.您的派生运算符根本不会覆盖抽象赋值运算符,它定义了一个完全正交的运算符,仅因为它具有相同的运算符名称.

C++ doesn't let you override virtual functions with covariant parameter types. Your derived operator doesn't override the Abstract assignment operator at all, it defines a totally orthogonal operator related only in that it's the same operator name.

创建此类函数时必须小心,因为如果两个实际派生类型不一致,则几乎可以肯定赋值将是无意义的.我会重新考虑是否可以通过替代方法更好地满足您的设计需求.

You have to be careful creating such functions because if the two actual derived types don't agree, almost certainly the assignment will be nonsensical. I would reconsider whether your design need could be served better by an alternate approach.

这篇关于派生类的虚拟赋值运算符未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-29 01:02