简体   繁体   English

无法使用base clase rvalue初始化指向子类的指针

[英]Cannot initialize pointer to a subclass with base clase rvalue

I have a question regarding a problem in a C++ exam. 我对C ++考试中的问题有疑问。 I had quite an argument with a teaching assistant and I want to know, if I missed something. 我和一位助教有很多争执,我想知道,如果我错过了什么。

Given is the following code: 给出以下代码:

class Student {
public:
  virtual void checkFinanes() {
    std::cout << "I have no money. \n";
  }
};

class ExonomyStudent : public Student {
public:
  virtual void checkFinances() {
    std::cout << "I am making a lot of money.\n";
  }
};

The question is: 问题是:

Does the following code compile? 以下代码是否编译? If so, what is the output on the console when i is expected. 如果是这样,当我需要时,控制台上的输出是什么。 If not, why not? 如果没有,为什么不呢?

Student student;
student.checkFinances();
EconomyStudent* economyStudent = &student;
economyStudent->checkFinances();

And the answer is supposed to be: 答案应该是:

Does not compile. 不编译。 Cannot initialize EconomyStudent * with Student rvalue. 无法使用学生rvalue初始化EconomyStudent *。

I am aware and understand, that one cannot initialize a pointer to a subclass with and address of an object of a baseclase and that this does not compile. 我知道并理解,不能初始化指向子类的指针和baseclase对象的地址,并且这不能编译。 What I don't understand is the rvalue. 我不明白的是右值。 Is it really important that rvalue is stated to make the answer valid? 声明rvalue以使答案有效是否真的很重要? My teaching assistant insisted but I don't think so. 我的助教坚持但我不这么认为。 What do you think? 你怎么看?

Is it really important that rvalue is stated to make the answer valid? 声明rvalue以使答案有效是否真的很重要?

I don't think so. 我不这么认为。 Check the case where you try to initialize EconomyStudent *economyStudent with an lvalue: 检查您尝试使用左值初始化EconomyStudent *economyStudent的情况:

 Student student;
 student.checkFinances();
 Student *lvalue = &student;
 EconomyStudent* economyStudent = lvalue;

clang complains on my machine, stating that clang抱怨我的机器,说明这一点

error: cannot initialize a variable of type 'EconomyStudent *' with an lvalue of type 'Student *' 错误:无法使用“Student *”类型的左值初始化“EconomyStudent *”类型的变量

Hence, the only difference in terms of compiler output is rvalue vs. lvalue. 因此,编译器输出方面的唯一区别是rvalue与lvalue。 As the value category has nothing to do with the type of error in the snippet, you can tell your teaching assistant that some random user on the internet thinks that you are right. 由于值类别与代码段中的错误类型无关,您可以告诉您的助教,互联网上的一些随机用户认为您是对的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用右值初始化智能指针 - Initialize smart pointer with rvalue 为什么编译器抱怨无法使用Base类型的右值初始化“派生” - why compiler complains cannot initialize “Derived” with an rvalue of type Base 为什么不能使用右值初始化左值引用? - Why a rvalue cannot be used to initialize a lvalue reference? 指向右值的指针 - pointer to rvalue C ++无法使用&#39;char&#39;类型的右值初始化&#39;char *&#39;类型的变量 - C++ cannot initialize a variable of type 'char *' with an rvalue of type 'char' 如何修复“无法使用'QGraphicsLayoutItem *'类型的右值初始化'QGraphicsProxyWidget *'类型的变量”? - How to fix "cannot initialize a variable of type 'QGraphicsProxyWidget *' with an rvalue of type 'QGraphicsLayoutItem *'"? 错误:无法初始化类型为&#39;bool&#39;的右值的类型&#39;Type *&#39;的返回对象 - error: cannot initialize return object of type 'Type *' with an rvalue of type 'bool' 无法使用“const Node *”类型的右值初始化“Node *”类型的变量 - cannot initialize a variable of type 'Node *' with an rvalue of type 'const Node *' 在给定指向其基类的指针的情况下识别子类? - Identifying a subclass given a pointer to its base class? 无法使用“int”类型的右值初始化“designFlags”类型的变量 - cannot initialize a variable of type 'designFlags' with an rvalue of type 'int'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM