简体   繁体   English

带有 C++ 中的指针和对象的“static_cast”

[英]"static_cast" with pointers and objects in C++

I've just learnt about inheritance and started using casting.我刚刚了解了 inheritance 并开始使用铸造。 While I was messing around trying to get to know the topic I found myself facing this problem which I couldn't explain.当我试图了解这个话题时,我发现自己面临着这个我无法解释的问题。 Here's the code:这是代码:

#include <iostream>
#include <string>
using namespace std;
__interface AbstractClass {
    void Eat()const;
    void Sleep()const;
    void Work()const;
    string Info();
};
class Employee : public AbstractClass {
private:
    string lastName;
    int age, salary;
    static int EmpCounter;
protected:
    string name;
public:
    Employee(string n = "Avto", string ln = "Chachandize", int a = 18, int s = 3000)
        : name(n), lastName(ln), age(a), salary(s) {
        EmpCounter++;
    }
    virtual ~Employee() {
        EmpCounter--;
    }
    static int getEmpCounter() {
        return EmpCounter;
    }
    void Eat()const override {
        cout << name << " Is eating" << endl;
    }
    void Sleep()const override {
        cout << name << " Is sleeping" << endl;
    }
    void Work()const override {
        cout << name << " Is doing his/her stuff" << endl;
    }
    string Info() override {
        string a = to_string(age);
        string s = to_string(salary);
        return name + ' ' + lastName + ' ' + a + ' ' + s + ' ';
    }   
};
int Employee::EmpCounter = 0;
class Developer : public Employee {
    string language;
public:
    Developer(string n = "Avto", string ln = "Chachandize", int a = 18, int s = 3000, string l = "C++") :
        Employee(n, ln, a, s), language(l) {}
    ~Developer()override = default;
    void Work()const override {
        cout << name << " Is writing code in " << language << endl;
    }
    string Info() override {
        Employee* emp = static_cast<Employee*>(this);
        //Employee emp = static_cast<Employee>(*this);
        return emp->Info() + ' ' + language;
    }
};
int main() {
    Developer dev;
    cout << dev.Info() << endl;
}

I was trying to upcast Developer to Employee and then get his info.我试图将Developer向上转换为Employee ,然后获取他的信息。 However static cast with pointers gives me error.但是使用指针转换的 static 给了我错误。

Strangely, second one which is commented doesn't.奇怪的是,被评论的第二个没有。 I don't know what is the reason of that.我不知道这是什么原因。 I also tried it with reference and there's also error.我也参考了它,也有错误。 Same thing happened while using dynamic cast.使用动态转换时发生了同样的事情。

string Info() override {
        Employee* emp = static_cast<Employee*>(this);
        //Employee emp = static_cast<Employee>(*this);
        return emp->Info() + ' ' + language;
    }

So my question is, is that supposed to be an error or not?所以我的问题是,这是否应该是一个错误?

As it was suggested you do not need to use casts when you want to get pointer to object to base class from the pointer to object to child class.正如建议的那样,当您想要从指向 ZA8CFDE6331BD59EB2AC96F8911C4B6666Z 到子 ZA8CFDE6331BD59EB2AC96F8911C4B6666Z 的指针获取指向 object 的指针到基础 class 的指针时,您不需要使用强制转换。 But it seems you want to call "Info" method of base class ("Employee") inside the body of "Info" method of the child class ("Developer").但是您似乎想在子 class(“开发人员”)的“Info”方法的主体内调用基础 class(“Employee”)的“Info”方法。 It can be done in the following way:可以通过以下方式完成:

Employee::Info();

for example in your case:例如在你的情况下:

string Info() override {
        return Employee::Info() + ' ' + language;
   }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM