0

I've just learnt about inheritance and started using casting. 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. However static cast with pointers gives me error .

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?

1 Answers1

0

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. But it seems you want to call "Info" method of base class ("Employee") inside the body of "Info" method of the child class ("Developer"). It can be done in the following way:

Employee::Info();

for example in your case:

string Info() override {
        return Employee::Info() + ' ' + language;
   }
Speechkin
  • 41
  • 1
  • Yeah I think that's the best solution however I am interested to know what exactly happens when we write like that – Avto Chachanidze May 16 '21 at 15:55
  • I don't think there are any problems from the casting perspective in your code. But it is redundant to use cast here, it is better to write (as suggested above) "Employee* emp = this;" It is not clear what kind of error did you face. But I may suppose that the method string Info() override { Employee* emp = static_cast(this); return emp->Info() + ' ' + language;} will go into infinite recursion because even if "emp" is a pointer to "Employee*" the Developer's implementation of Info will we choosen because "Info" is virtual method in emp->Info() is called. – Speechkin May 16 '21 at 16:13
  • `string Info() override` should fail to compile, since `Info` is not declared `virtual` at any stage. (But the code uses non-standard extension `__interface` which IDK about; the MS documentation says it does not change the class behaviour, just is meant to give a warning if the class contains non-virtual funtions etc.) – M.M May 18 '21 at 01:31