0

I have

struct Geometry_Base
{
//snipped data
virtual unsigned char GetID(){return 0u;}
}
struct Geometry_Child : Geometry_Base
{
//snipped data
unsigned char GetID() override {return 1u;}
}

struct Cell
{
//snipped
Geometry_Base geometry;
}

Then later on I do:

Geometry_Child child;
Cell cell;
cell.geometry = child;
unsigned char ID = cell.geometry.GetID();

This copies all the data over from child to cell.geometry, but the function being called is that of the base class. What is it that I'm doing wrong?

Nnimrod
  • 1
  • 1
  • 1
    Polymorphism requires some level of abstraction to work, usually pointers. `Geometry_Base geometry;` means `geometry` is and will always be a `Geometry_Base`. `cell.geometry = child;` just copies the `Geometry_Base` portion of `child` to `cell.geometry`. This is called Object Slicing, the assignment "slices" the derived part off. – François Andrieux Sep 02 '22 at 20:40

0 Answers0