【发布时间】:2011-10-30 18:50:29
【问题描述】:
我对 C++ 还很陌生,并且正在尝试掌握虚拟赋值。下面的程序由一个具有两个数据成员的抽象基类和一个具有一个数据成员的派生类组成。当我设置一个指向派生对象的抽象指针时,程序使用 operator= 的抽象版本而不是派生版本,即使它们都被声明为“虚拟”。我在这里做错了什么?
提前致谢,
杰
#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 << "\nrating: "
<< rating << "\nstyle: " << 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;
}
【问题讨论】:
-
不相关,但你为什么使用
char*s? -
不深入细节,你做错了。阅读learncpp.com/cpp-tutorial/…,并在谷歌搜索“虚拟赋值运算符”。这已经讨论了很多了。
-
为了缩短代码,我尝试删除与我的问题无关的内容,包括动态内存(因此是 char *s)
-
嗨,Seth,我已经花了很多时间搜索谷歌(和这个网站)。我已经看过你链接到的页面了。
-
*ptr部分取消引用指针以获取实际对象,然后在该对象上调用operator=。不发生虚方法查找。