0

I'm trying to make a c++ variable of vector<Superclass>.

I originally gave the Superclass a public function in the header: virtual int function(); that I attempted to override in two subclasses like such:

--in the header--

class Subclassthing : public Superclass { public: int function(); ...}

--in the cpp file--

int Subclass::function() {//actually do stuff}

I later create a vector<Superclass> that I add Subclass elements into and call function() on.

The build screamed at me for some insane reason that led me to some hex codes when I tried to see why. It got stopped breaking when I instead changed the declaration in the superclass to virtual int function() {return 0;} , but I suspect that I'm not doing this properly and that it will only call the Superclass's version as opposed to the subclass's. How do I fix this? At first I tried to make the superclass declaration a pure virtual function but then I was no longer able to create a vector of it, since I can't instantiate pure virtual objects.

edit: ok I realize I'm getting screwed by slicing now. does this mean that there's no way to do this elegantly (er, without pointers?)

Milo Hou
  • 239
  • 2
  • 17

0 Answers0