Class casting

Hello!

I have started with C++ and I have a question regarding class casting in c++.

Let's say I have 2 classes and one extends the other:

1
2
3
4
5
6
7
8
9
10
class Person {
public:
    Person();
    Person(const Person& orig);
    virtual ~Person();
    int getAge();
    void setAge(int age);
private:
    int age;
};


and

1
2
3
4
5
6
7
class MyPerson : public Person {
public:
    MyPerson();
    MyPerson(const MyPerson& orig);
    virtual ~MyPerson();
    int getAge();
};


where funtion getAge() is only a call to Person:

1
2
3
int MyPerson::getAge(){
    return Person::getAge();
}


In some part of my code I am doing a static_cast from Person to MyPerson:

1
2
Person p = getFromSomeWhereAPerson();
MyPerson mp = static_cast<MyPerson>(p);


I think is not save, because a Person must not be a MyPerson instance but in this case, MyPerson is only calling its super class Person...and compiles and I don't have (at the moment) segmentation faults :)

In order to avoid the static_cast, I have thought in 2 solutions:

1) Copy manually all properties from a Person to a MyPerson:

1
2
3
4
5
6
7
8
9
class MyPerson : public Person {
public:
    MyPerson();
    MyPerson(const MyPerson& orig);
    virtual ~MyPerson();
    int getAge();
       // Copy state from Person
    void loadFromPerson(Person p);
};


where
1
2
3
int MyPerson::loadFromPerson(Person p){
    Person::setAge(p.getAge());
}


2) Another idea is to convert MyPerson in a wrapper:
1
2
3
4
5
6
7
8
9
10
class MyPerson : public Person {
public:
    MyPerson();
    MyPerson(const MyPerson& orig);
    virtual ~MyPerson();
    const Person* getDelegate();
    int getAge();
private:
    Person * _delegate;
};


where getAge() is a call to its wrapped Person:
1
2
3
int MyPerson::getAge(){
    return _delegate->getAge();
}


I don't really know what to do and I think this is a common situation....any suggestion?

Than you in advance!
I'm not sure how you ended up here to begin with.

If you're doing object oriented programming, you should be using pointers. You have a virtual descructor in Person, which implies OO, but you're not using those classes correctly. Further more, the treatment of getAge() isn't quite right. Why isn't it virtual?

You code:
1
2
Person p = getFromSomeWhereAPerson();
MyPerson mp = static_cast<MyPerson>(p);
is problematic in itself.

Consider this alternative:
1
2
Person* p = some_factory();  // Get some instance of a Person, it could be derived class.
MyPerson* mp = dynamic_cast<MyPerson*>(p);  // We need a typesafe downcast as we may have a pointer to some other type of Person 
Last edited on
Thank you for you quick response!


I have tried to minimize my current problem with Person an MyPerson classes. I am really working with more complexes classes and structures .

If you're doing object oriented programming, you should be using pointers


Complete agree. My problem is how to solve the down casting. Let's say that the example is how you have posted:

1
2
Person* p = some_factory();  
MyPerson* mp = dynamic_cast<MyPerson*>(p);


Further more, the treatment of getAge() isn't quite right. Why isn't it virtual?


In my real situation, Person is a class from a library and I need to extend as it is. It is a condition not a decision.

I understandt your response with dynamic_cast. It is a save casting. But, what can I do when I want to cast from Person to MyPerson? Or maybe I should change the question to how can I create a new MyPerson from a Person?
Topic archived. No new replies allowed.