With the base class pointer, we cannot access the elements of a specific class

A base class pointer can point to a derived class object. Why is it wrong?

This thread says that we cannot make a derived point of a class a base class because a derived class might not have access to all members of the base class.

The same is true the other way around. The base class won't even have access to members that are derived from the class.

#include <iostream>

using namespace std;

class BaseClass
{
public:
    virtual void baseFx();
};

void BaseClass::baseFx()
{
    std::cout << "From base virtual function\n";
}

class DerivedClass : public BaseClass
{
public:
    void baseFx();
    void h() { std::cout << "From derived class. h()\n"; }
};

void DerivedClass::baseFx()
{
    std::cout << "From derived virtual function\n";
}

int main()
{
    BaseClass* objBaseclassB = new DerivedClass();
    objBaseclassB->baseFx (); // Calls derived class virtual function.
    objBaseclassB->h(); // error: 'class BaseClass' has no member named 'h'.

    return 0;
}

      

So why is the restriction only for the derived class? Why is this not allowed given the above logic? DerivedClass* objBaseclassC = new BaseClass();

+3


source to share


2 answers


Assigning a Derived to a base class pointer is a valid and cornerstone of polymorphism.

Yes, the Derived class can have additional members that cannot be accessed using the base class pointer, which is good, because you, the programmer, are doing such a task, being fully aware that you cannot access these members . You are doing this solely for the purpose of polymorphism. This means that you will only call virtual functions that vtables will use to call the correct override based on what object the pointer is pointing to.

However, casting a base class pointer back to a derived class pointer is not straightforward. You need type information, and this is only possible if the Base class is polymorphic (i.e. has virtual functions) and the class you are executing is derived from the base class. This exact check is done dynamic_cast

. dynamic_cast

checks if downcast is possible and returns the Derived class pointer if valid. Otherwise, it returns nullptr

.



Coming to your final question: DerivedClass* objBaseclassC = new BaseClass();

not valid as accessing any "extra" members in DerivedClass crashes.

But when you do this BaseClass* derivedPointer = new DerivedClass();

This kind of construction will lead to a compile-time error.

+1


source


Let me explain it logically.

Inheritance in OO programming is used to implement the is relationship between different classes. For example. If there is an Animal class and a Dog class, we know that "Dog" is "Animal" and therefore Dog extends Animal. Hence, it would obviously display more functionality (like eat () - function) that is common among other animals (classes) like Horse. Thus, we tend to inject such generic code into the base Animal class, and keep the code for each subclass in derived classes. (For example, bark () for a dog)



Hence, it only makes sense to let the base class type pointer point to the derived class object, i.e. Animal * horse1 = new Horse (); because, after all, a horse is an animal. Another way doesn't make sense, since every Animal is not necessarily a horse.

PS: As far as C ++ is concerned, inheritance is basically just a reuse mechanism for shared code.

0


source







All Articles