1

I am having some issues with virtual functions. I have the following two structures (simplified):

struct Shape
{
public:
   virtual std::vector<unsigned int> GetIndices()
   {
      // standard definition
      return { 0, 1, 2 };
   }
};

struct Cube : public Shape
{
public:
   std::vector<unsigned int> GetIndices()
   {
      return { 0, 1, 2, 2, 3, 0 };
   }
};

Then, there's a function that takes a Shape as the argument. It calls the GetIndices Function. It looks like this:

void Add(Shape shape)
{
   // ...
   std::vector<unsigned int> indices = shape.GetIndices();
}

When I call this function with a Cube as the Argument, somewhat like this:

Cube cube1;
Add(cube1);

shape.GetIndices(); in the Add() Function will return { 0, 1, 2 }, but I want it to return { 0, 1, 2, 2, 3, 0 }. What am I doing wrong?

muhmann
  • 191
  • 9

0 Answers0