继承此C++代码有什么问题

what is the problem with Inheritance of this C++ code

本文关键字:什么 问题 代码 C++ 继承      更新时间:2023-10-16

这是我的代码:

class SimpleProduct {
char look = 'x';
string name = "Undefined";
string type = "Undefined";
string description = "Undefined";
public:
char getLook() const {return look;}
string getType() const {return type;}
string getName() const {return name;}
string getDescription() const {return description;}
SimpleProduct(char look = 'x', string &&name = "Undefined", string &&type = "Undefined", string &&description = "Undefined");
string toString() const;
};
class TallProduct : public SimpleProduct {
public:
TallProduct(char look, string &&name = "Undefined", string &&type = "Undefined", string &&description = "Undefined");
string toString() const;
};

inline SimpleProduct::SimpleProduct(char look, string &&name, string &&type, string &&description) :
look(look), name(move(name)), type(move(type)), description(move(description)) {
}
inline string SimpleProduct::toString() const {
ostringstream ost;
if (this->getName() == "Space") {
ost << "";
} else {
ost << "Look: " << this->getLook() << ", name: " << this->getName() << ", type: " << this->getType() << ", description: "
<< this->getDescription();
}
return ost.str();
};
inline TallProduct::TallProduct(char look, string &&name, string &&type, string &&description){
}
inline string TallProduct::toString() const {
return "TALL | " + SimpleProduct::toString();
}

这是我的测试:

TEST(Test3, TallProduct) {
SimpleProduct *product = new TallProduct('t', "Fresh sandwich", "sandwich", "check the expiration date");
ASSERT_EQ("TALL | Look: t, name: Fresh sandwich, type: sandwich, description: check the expiration date",  product->toString());
}

我得到的结果总是这样的:

"Look: x, name: Undefined, type: Undefined, description: Undefined"

虽然它应该是:

"TALL | Look: t, name: Fresh sandwich, type: sandwich, description: check the expiration date"

你能告诉我我在哪里犯了错误吗?我想错误的部分是在调用方法::tostring,但不知道如何调用TallProduct::toString,而不是SimpleProduct::toString

TallProduct构造函数不初始化其基类。 写入

inline TallProduct::TallProduct(char look, string &&name, string &&type, string &&description) : 
SimpleProduct::SimpleProduct(look, std::move(name), std::move(type), std::move(description))
{
}

请参阅委派构造函数。

此外,基类的函数不是虚拟的。这意味着,对product->toString()的调用将始终调用SimpleProduct::toString,因为productSimpleProduct *类型,即使指向的对象是TallProduct类型。如果函数被声明为virtual,程序将在运行时找出要调用什么。

此外,还必须声明基类 virtual 的析构函数。在上面的链接中阅读有关此内容的信息。否则,将调用未定义的行为。

这是多态性的典型案例。您的*product指针变量是SimpleProduct类型,但您已为其分配了一个TallProduct对象,并假定调用product->toString()将调用toString()TallProduct版本。事实并非如此。

这称为编译时/静态绑定。这意味着在编译过程中,编译器将product->toString()调用绑定到toString()SimpleProduct版本,因为这是合乎逻辑的决定,对吗?

但是,如果您将toString()方法定义为SimpleProduct中的virtual,并且TallProduct那么编译器会说:"好吧,我现在不会将任何方法绑定到此调用,我们将在运行时看到实际调用哪个方法取决于*product引用的实际变量类型。

阅读更多关于C++多态性的信息。

阅读有关静态和动态绑定的更多信息 虚拟函数(仅限C++(

问题的另一半是:由于TallProduct没有初始化SimpleProductSimpleProduct中的所有类变量都有默认值。

char look = 'x';
string name = "Undefined";
string type = "Undefined";
string description = "Undefined";

因此,ASSERT_EQ宏的参数不相等