How do I cast a parent class as the child class

后端 未结 8 1928
深忆病人
深忆病人 2020-12-29 19:46

It\'s been a while since I have had to write C++ code and I\'m feeling kind of stupid. I\'ve written code that is similar to, but is not exactly, the code b

相关标签:
8条回答
  • 2020-12-29 20:20

    You cannot cast the actual objects, but you can cast pointers to the objects.

    To cast pointers use code like this:

    Child* c = reinterpret_cast<Child*>(p);
    
    0 讨论(0)
  • 2020-12-29 20:25

    You can cast using Single Argument Constructor: i.e. (A parent works, A child Studies) as follows:

    #include <iostream>
    using std::cout;
    
    class Parent
    {
        public:
        void goToWork()
        {
            cout<<"working\n";  // only parents work
        }
    };
    
    class Child : public Parent
    {
        public:
        Child(const Parent& parentAddr){}
        void goToSchool()
        {
            cout<<"studying\n";   // only children studies
        }
    };
    
    int main(void)
    {
        Child child(*(new Parent()));
    
        // here's a child working
        child.goToWork();
        return 0;
    }
    

    you pass an child class address as parent's constructor parameter and you can use a child obj to do parent's stuff

    0 讨论(0)
  • 2020-12-29 20:27

    You cannot cast an object of parent class to child class type. An object of parent class is... well, an object of parent class. Child class extends parent class, meaning that an object of parent class is generally "smaller" than an object of child class. For this reason casting (or reinterpreting) parent class as child class makes no sense whatsoever.

    Explain what is it you are trying to do. Without an explanation, your question simply makes no sense.

    0 讨论(0)
  • 2020-12-29 20:34

    You probably don't want to be casting here at all. If Parent has any abstract methods you just call them and the derived class will automatically handle them correctly.

    There are times where you link relatively unrelated items together just so you can store them in a collection, either variant types or situations where different state leads to unrelated objects which are handled differently, and on those occasions you might want to cast.

    I am quite surprised, by the way, that you didn't get a compiler error on GetThing() because you have declared c as a function so you are not returning a Parent.

    In addition, by the way, if you copy by value you will "slice" thus:

    Child c;
    Parent p(c);
    Child & c2 = dynamic_cast< Child& >(p); // throws bad_cast
    
    0 讨论(0)
  • 2020-12-29 20:35

    A Parent object returned by value cannot possibly contain any Child information. You have to work with pointers, preferably smart pointers, so you don't have to clean up after yourself:

    #include <memory>
    
    class Factory
    {
        // ...
    
    public:
    
        static std::unique_ptr<Parent> GetThing()
        {
            return std::make_unique<Child>();
        }
    };
    
    int main()
    {
        std::unique_ptr<Parent> p = Factory::GetThing();
        if (Child* c = dynamic_cast<Child*>(p.get()))
        {
            // do Child specific stuff
        }
    }
    
    0 讨论(0)
  • 2020-12-29 20:39

    You can't, really. Your factory has returned a Parent object, which was constructed from the Child object c[*]. The Child part of it has already been sliced away, as it's returned to the main function. There's no way to recover it.

    Perhaps you want to use pointers?

    [*] Except that, Child c(); declares a function, it doesn't define an object. But this isn't your real code, and I guess your real class has constructor parameters.

    0 讨论(0)
提交回复
热议问题