Base class pointer vs inherited class pointer?

后端 未结 8 1125
独厮守ぢ
独厮守ぢ 2021-01-05 23:11

Suppose I have a class Dog that inherits from a class Animal. What is the difference between these two lines of code?

    Animal *a         


        
相关标签:
8条回答
  • 2021-01-05 23:47

    No, they aren't the same.

    The Dog pointer is not as polymorphic as Animal. All it can point to at runtime is a Dog or a subclass of Dog. If there are no subclasses of Dog, then the Dog runtime type and compile time types are the same.

    The Animal pointer can refer to any subclass of Animal: Dog, Cat, Wildebeast, etc.

    0 讨论(0)
  • 2021-01-05 23:49

    You must always remember there are 2 parts in every class, the data and the interface.

    Your code truly created 2 Dog objects on the heap. Which means the data is of Dog. This object is of size the sum of all data members Dog + Animal + the vtable pointer.

    The ponters a and d (lvalues) differ as from a interface point of view. Which determines how you can treat them code wise. So even though Animal* a is really a Dog, you could not access a->Bark() even if Dog::Bark() existed. d->Bark() would have worked fine.

    Adding the vtable back into the picture, assuming the interface of Animal had Animal::Move a generic Move() and that Dog really overwriten with a Dog::Move() { like a dog }.

    Even if you had Animal a* and performed a->Move() thanks to the vtable you would actually Move() { like a dog }. This happens because Animal::Move() was a (virtual) function pointer re-pointed to Dog's::Move() while constructing Dog().

    0 讨论(0)
  • 2021-01-05 23:50

    The answer to this question is a giant: It depends

    There are numerous ways in which the type of the pointer could become important. C++ is a very complex language and one of the ways it shows up is with inheritance.

    Lets take a short example to demonstrate one of the many ways in which this could matter.

    class Animal {
    public:
      virtual void MakeSound(const char* pNoise) { ... }
      virtual void MakeSound() { ... }
    };
    
    class Dog : public Animal {
    public:
      virtual void MakeSound() {... }
    };
    
    int main() {
      Animal* a = new Dog();
      Dog* d = new Dog();
      a->MakeSound("bark");
      d->MakeSound("bark"); // Does not compile
      return 0;
    }
    

    The reason why is a quirk of the way C++ does name lookup. In Short: When looking for a method to call C++ will walk the type hierarchy looking for the first type which has a method of the matching name. It will then look for a correct overload from the methods with that name declared on that type. Since Dog only declares a MakeSound method with no parameters, no overload matches and it fails to compile.

    0 讨论(0)
  • 2021-01-05 23:53

    The first line allow you to call only members of the Animal class on a :

    Animal *a = new Dog();
    a->eat(); // assuming all Animal can eat(), here we will call Dog::eat() implementation.
    a->bark(); // COMPILATION ERROR : bark() is not a member of Animal! Even if it's available in Dog, here we manipulate an Animal.
    

    Although (as pointed by others), in this cas as a is still an Animal, you can't provide a as a parameter of a function asking for a more specific child class that is Dog :

    void toy( Dog* dog );
    
    toy( a ); // COMPILATION ERROR : we want a Dog!
    

    The second line allow you to use specific functions of the child class :

    Dog *a = new Dog();
    a->bark(); // works, but only because we're manipulating a Dog
    

    So use the base class as the "generic" interface of your class hierarchy (allowing you to make all your Animals to eat() whithout bothering about how).

    0 讨论(0)
  • 2021-01-06 00:00

    The distinction is important when you call a virtual function using the pointer. Let's say Animal and Dog both have functions called do_stuff().

    1. If Animal::do_stuff() is declared virtual, calling do_stuff() on an Animal pointer will call Dog::do_stuff().

    2. If Animal::do_stuff() is not declared virtual, calling do_stuff() on an Animal pointer will call Animal::do_stuff().

    Here's a full working program to demonstrate:

    #include <iostream>
    
    class Animal {
    public:
            void do_stuff() { std::cout << "Animal::do_stuff\n"; }
            virtual void virt_stuff() { std::cout << "Animal::virt_stuff\n"; }
    };
    
    class Dog : public Animal {
    public:
            void do_stuff() { std::cout << "Dog::do_stuff\n"; }
            void virt_stuff() { std::cout << "Dog::virt_stuff\n"; }
    };
    
    int main(int argc, char *argv[])
    {
            Animal *a = new Dog();
            Dog *b = new Dog();
    
            a->do_stuff();
            b->do_stuff();
            a->virt_stuff();
            b->virt_stuff();
    }
    

    Output:

    Animal::do_stuff
    Dog::do_stuff
    Dog::virt_stuff
    Dog::virt_stuff
    

    This is just one example. The other answers list other important differences.

    0 讨论(0)
  • 2021-01-06 00:02

    It makes no real difference at run time, as the two instances are the same. The only difference is at compile time, where you could call for example d->bark() but not a->bark(), even if a actually contains a dog. The compiler considers the variable to be an animal and only that.

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