Mr.Anubis
Mr.Anubis

Reputation: 5312

Virtual function acting weird in inheritance?

Lemme explain my problem by giving an example:

#include <iostream>

class PC
{
public:
    PC():Data(0)
    {
    }
    virtual void display()
    {
        std::cout<<"The data is :"<<Data<<std::endl;
    }
protected:
    int Data;
};

class SmartPC:private PC
{
public:
    SmartPC():PC()
    {
    }
    void convert()
    {
        PC* temp=static_cast<PC*>(this);
        temp->display();
    }
    void display()
    {
        std::cout<<"The data is (in bb):"<<a<<std::endl;
    }
};

int main()
{
    SmartPC SmrtPC;
    PC* miniPC= static_cast<PC*>(&SmrtPC);
    SmrtPC.convert();
}

According to Scott Meyers : static_cast<PC*>(this); will create a temp base copy of SmartPC. But temp->display(); executed the display() function of derived class. Why is that so? Shouldn't it execute the function of base's display(), since the object is now completely a copy of base of SmartPC?

Another question is that if I add the line temp->data; in convert() function , it says PC::Data is protected but I am accessing it from derived class scope i.e SmartPC, so why doesn't it work?

Any help is appreciated.

Upvotes: 1

Views: 185

Answers (2)

Matteo Italia
Matteo Italia

Reputation: 126777

According to scott meyers : static_cast<PC*>(this); will create temp base copy of SmartPC. but temp->display(); executed the display() function of derived class why is it so? it should execute function of base's display() , since the object is now completely a copy of base of SmartPC.

No copy is created, you are only casting a pointer.

Since the class is polymorphic, calling a virtual function via a pointer results in calling the "correct" version of the function (according to the dynamic type of the object, which is SmartPC *).

If, instead, display was not virtual, the version of the base class would have been called, since for nonvirtual methods it's the static type of the pointer to determine which version is to be called.

(display is virtual also in SmartPC even if it's not explicitly specified, the virtual qualifier is implied when overriding inherited virtual functions)


Notice instead that if you did:

PC temp(*this);

you would have actually created a copy of the current object, "sliced" to be an object of type PC. This is called "object slicing" and is performed by the copy constructor of PC; most often this is undesired behavior (because what was an object of the derived class actually becomes an object of the base class, and polymorphism do not work as some may expect).


Another question is that if i add the line temp->data; in convert() function , it says PC::Data is protected but i am accessing it from derived class scope i.e SmartPC, so why doesn't it works?

Conceptually, when you try to access temp->data you're trying to access a private member of another object (it's not important that temp is actually this), so the access is denied. Your "derived class privileges" to access protected members work only on this.

Upvotes: 5

Puppy
Puppy

Reputation: 146910

It is not a copy of the class, it is a reference to the base class object, and as such, it behaves polymorphically like any other.

The reason you can't access the protected members is because you used private inheritance.

Upvotes: 1

Related Questions