继承此C++代码有什么问题
what is the problem with Inheritance of this C++ code
这是我的代码:
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
,因为product
是SimpleProduct *
类型,即使指向的对象是TallProduct
类型。如果函数被声明为virtual
,程序将在运行时找出要调用什么。
此外,还必须声明基类 virtual 的析构函数。在上面的链接中阅读有关此内容的信息。否则,将调用未定义的行为。
这是多态性的典型案例。您的*product
指针变量是SimpleProduct
类型,但您已为其分配了一个TallProduct
对象,并假定调用product->toString()
将调用toString()
的TallProduct
版本。事实并非如此。
这称为编译时/静态绑定。这意味着在编译过程中,编译器将product->toString()
调用绑定到toString()
的SimpleProduct
版本,因为这是合乎逻辑的决定,对吗?
但是,如果您将toString()
方法定义为SimpleProduct
中的virtual
,并且TallProduct
那么编译器会说:"好吧,我现在不会将任何方法绑定到此调用,我们将在运行时看到实际调用哪个方法取决于*product
引用的实际变量类型。
阅读更多关于C++多态性的信息。
阅读有关静态和动态绑定的更多信息 虚拟函数(仅限C++(
问题的另一半是:由于TallProduct
没有初始化SimpleProduct
SimpleProduct
中的所有类变量都有默认值。
char look = 'x';
string name = "Undefined";
string type = "Undefined";
string description = "Undefined";
因此,ASSERT_EQ
宏的参数不相等
- 警告处理为错误这里有什么问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 当我尝试添加 2 个大字符串时,我无法弄清楚出了什么问题
- 违反const正确性:我应该现实地期待什么问题
- 这个带有模板<类 Vector 的C++代码片段有什么问题>
- 我的逻辑反转字符串中的元音有什么问题?
- 需要以下代码的帮助,下面的代码有什么问题
- 常量公共成员有什么问题?
- 以下代码中的函数模板有什么问题?
- 这个返回元素位置的基于循环的函数有什么问题?
- creat_list2功能有什么问题?
- 格式说明符C++有什么问题
- 任何人都可以告诉我我的 C++ 代码出了什么问题?
- 从 argv[1] 转换为字符 * 字符串后有什么问题?
- 我的堆栈和库存清单程序的结构有什么问题?
- 此工厂功能有什么问题?
- 以下 C++ 代码有什么问题?
- 数组为此合并排序函数提供了正确的输出,但向量给出了不正确的输出.出了什么问题?
- reinterpret_cast,只读访问,简单的可复制类型,会出什么问题?
- 它解决了什么问题,对于非真空初始化,生命周期在初始化之前就开始了