#Polymorphism with pointers and references

22 messages · Page 1 of 1 (latest)

nocturne ledge
#

So object slicing is when you do copy by value and its completely lost, but when I do polymorphism with pointers and references, the extra parts are also technically lost until I cast the pointer. So if I have Base-> Derived-> Derived2, and I store a pointer to a Derived2 object into an object that holds a pointer to Derived, then I would only be able to access Base and derived members and functions and virtual functions of Derived2 correct? The only way i would be able to access derived2 members and functions would be to cast it?

class Base{
  public:
    int x;
    int y;
    void someFunc.....;
    virtual void vFunc....;
}

class Derived: public Base{
  public: 
    int z;
    int s;
    void someFunc2....;
}

class Derived2: public Derived{
  public:
    int o;
    void vFunc......;
}

int main(){
  Derived* ptr = new Derived2();
}

I wouldnt be able to do ptr->o; but i can access everything else? from base to derived and including derived2 virtual functions

tawdry pollenBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

cinder bear
#

Can you show an example of what you're talking about in code?

cinder bear
#

You cannot access the member variables that were added in Derived2, but you can call any functions in Derived or Base that were marked as virtual, without losing any information

#

Derived* ptr basically points to the subobject Derived inside of Derived2

#

The compiler knows that any virtual functions here are potentially overloaded in Derived2 so it will generate the proper virtual dispatch to find the correct function

eternal bay
#

It's worth noting: This is something different than object slicing. That actually does lose information, as the newly created object no longer has all the data that the original object had.

nocturne ledge
eternal bay
#

The data is not lost. You just lose (direct) access to it.

cinder bear
#

It only loses the fact that ptr is definitively pointing to a larger Derived2 object

nocturne ledge
#

so it's temporaily lost unless i access it througha virtual function

eternal bay
#

I would get rid of the mentality that it's "lost"

cinder bear
#

Kinda, the compiler knows that it is definitely a virtual function so it knows that it may be overriden by a larger derived object

#

So the compiler knows it must produce a virtual lookup

nocturne ledge
#

gotcha makes sense

#

so if in derived2, i have the override function, i can still access o in there

eternal bay
#

Yes. But you can't directly access Derived2::o from Derived ptr

nocturne ledge
#

gotcha okay

eternal bay
#

It's just changing the visibility of members, effectively

nocturne ledge
#

so this is essentially what's happening in memory:
Derived2 ptr: {Base Derived Derived2}
Derived ptr: {Base Derived} Derived 2

(plus virtual look up for both)

basically just the viewable window of the memory changes based on the ptr type?

cinder bear
#

Yes