- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
试图了解实例化派生类对象的行为差异
I created a base class "person" and a derived class "employee" as below
人
class person
{
protected:
string name;
public:
person();
~person();
virtual void setName(string myName)
{
name = myName;
}
};
员工
class employee : public person
{
public:
employee();
~employee();
void setName(string myName)
{
name = myName+"さん";
}
};
int main()
{
person newPerson = person();
employee anotherPerson1 = employee();
employee* anotherPerson2 = new employee();
person extraPerson1 = employee();
person* extraPerson2 = new employee();
newPerson.setName("new");
anotherPerson1.setName("another1");
anotherPerson2->setName("another2");
extraPerson1.setName("extra1");
extraPerson2->setName("extra2");
cout << newPerson.getName() << endl;
cout << anotherPerson1.getName() << endl;
cout << anotherPerson2->getName() << endl;
cout << extraPerson1.getName() << endl;
cout << extraPerson2->getName();
}
new
another1さん
another2さん
extra1
extra2さん
我了解 newPerson、anotherPerson1 和 anotherPerson2 的行为。
I fail to understand why extraPerson1 and extraPerson2 behave differently even though both seem to have similar initiations.
求助!
最佳答案
与
person extraPerson1 = employee();
你slice employee
对象转换为 person
对象。对象 extraPerson1
是一个 person
对象,而不是一个 employee
对象。当您调用它的 setName
函数时,您正在调用 person::setName
。
多态性和虚函数只有在你有指针或引用时才有效。
关于c++ - 新的运营商 ->有或没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55276012/
这个问题已经存在: 关闭 13 年前。 Possible Duplicate: What is the !! (not not) operator in JavaScript? 我见过运算符 ! 像
我最近正在读《行动中的格鲁夫》。在第7章中,它介绍了*。运算符(operator) 。当我运行有关此运算符的代码时,我会遇到一些错误。 class Invoice {
我在沙箱 Groovy 上工作,我想阻止 .@ 运算符。我正在使用 SecureASTCustomizer 并编写了一个自定义的 SecureASTCustomizer.ExpressionCheck
我有一个函数接受一个包含大量数据的数组,例如,其中一个数据是“name”:$data = ['name' => 'John'] ; 现在,如果我想使用默认值从中获取名称,我有两个经常使用的选项。 $n
我只是想知道这是否是 C89 程序的“好”代码。 obj_ptr = (obj*) (ptr1 || ptr2); 本质上,如果 ptr1 != NULL 和 ptr2,它的作用(至少在我的计算机上的
我定义了一个类 User,并覆盖了它的 == 运算符,如下所示: class User attr_reader :age def initialize age @age = age
这个问题在这里已经有了答案: Why does "++x || ++y && ++z" calculate "++x" first, even though operator "&&" has hi
pipe(|) 和output(>) 重定向运算符有什么区别?我可以在哪里使用它们? 例如: 我通常只在 grep 中使用管道 (|) find . | grep abc 我遇到的输入输出重定向的唯一
我正在为此代码使用 g++ 10.2。有谁知道为什么我在最后一次 std::views::reverse 中收到编译器错误?在 results3 ? #include #include int m
假设我们正在使用 MASM 6.1/16 位/大数据模型编写汇编代码,并且我们有一个名为 MY_VAR 的变量(标签)、一个名为 MY_SEG 的段和一个名为 MY_GROUP 的段组。假设 MY_V
我试图在基类中使用宇宙飞船运算符,所以我将通过编译器定义所有运算符。 (来源:https://devblogs.microsoft.com/cppblog/simplify-your-code-wit
我在某处看到这段代码片段: var idx = SOME_VALUE; var color = { yellor: 1, red: 2, black: 0 }; var x = color
我观察到在某个时候有一个 ?海湾合作委员会的运营商。我如何在 GCC 4.5 下使用它们?它们是否已被移除,如果是,何时移除? Offset block_count = (cpfs->geo.bloc
我不是每天都阅读 C#,而是正在阅读最近的提交,其中包含一些让我感到困惑的内容: if (( length > maxLength) && ! IsTable(furniture.FurnitureT
拥有 public void Main() { int i = 0; Console.WriteLine($"I was {i++}, now I is {i}"); bool b = t
我观察到在某个时候有一个 ?海湾合作委员会的运营商。我如何在 GCC 4.5 下使用它们?它们是否已被移除,如果是,何时移除? Offset block_count = (cpfs->geo.bloc
今天,我听说了 Karatsuba 算法,一种快速乘法算法。我很好奇这个“快”是什么意思? 通常,我们在计算一段代码的时间复杂度时,会考虑使用 * 运算符作为 O(1) 的乘法运算,如果它始终为真,那
我观察到在某个时候有一个 ?海湾合作委员会的运营商。我如何在 GCC 4.5 下使用它们?它们是否已被移除,如果是,何时移除? Offset block_count = (cpfs->geo.bloc
我正在学习 Angular 和 RxJS 运算符。我正在使用 Hearthstone API 来训练它。我究竟做错了什么 ?我想过滤流以仅返回“萨满”卡。我知道,Api 让我有机会按类(class)获
正如标题所说,我正在尝试通过 += 运算符将文本附加到隐式解包的可选字符串,这给了我 'String!' is not identical to 'UInt8' 让你明白我的意思: var myStr
我是一名优秀的程序员,十分优秀!