4

Here is the code:

#include <iostream>
#include <vector>
#include <array>

class Parent
{
public:
    virtual void whatAmI(){std::cout << "A Parent" << std::endl;}
    virtual long getValue(){std::cout << "value from Parent " << std::endl; return value;}
    long value;
};

class Child : public Parent
{
public:
    virtual void whatAmI(){std::cout << "A child" << std::endl;}
    virtual long getValue(){std::cout << "value from Child " << std::endl; return value;}
    long value;
};

class SomeClass
{
public:
    Parent * parent;
};

int main()
{

Child c = Child();
SomeClass sc;

sc.parent = &c;
sc.parent->value = 10;
sc.parent->whatAmI();

std::cout << sc.parent->value << std::endl;
std::cout << sc.parent->getValue() << std::endl;
}

It returns:

A child
10
value from Child 
0

I have read about object slicing, and made sure I would assign the value of 10 after the child has been sliced up. I still don't understand why the direct field access and the function call would give different results.

Thank you.

Rrr
  • 53
  • 4
  • Child has 2 members `long value;`.(its own, and the one from its base `Parent`). – Jarod42 Mar 29 '17 at 15:34
  • Thank you, and to Olivier Chalesworth. I get it now. Does that mean that a Child takes twice the space in memory ? – Rrr Mar 29 '17 at 15:40
  • You can check size taken with `sizeof`: `sizeof(Parent)` and `sizeof(Child)`. As the classes are polymorphic you will have the overhead (*vtable* or equivalent) so extra space, yes, twice, probably not. – Jarod42 Mar 29 '17 at 15:48
  • 16 bytes for the parent, 24 for the child. – Rrr Mar 29 '17 at 16:00

1 Answers1

6

There is no slicing here - you're accessing via a pointer.

The behaviour is due to the fact that member-variable access is not polymorphic. So parent->value always refers to Parent::value, never Child::value. Whereas value (in Child::getValue) refers to Child::value.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680