I’m having a little problem with inheritance in C++ that I can’t quite figure out.
So, say I have some classes derived from stl list, i.e:
class Class1: public list<T>{
virtual func1();
}
class Class2 : public Class1<w/e>{
func1();
}
The problem I’m having is when passing these to a function. How can I properly pass an instance of one of these to a function so that it will use the proper virtual function if it is possible for an instance of either type to be passed? I haven’t done inheritance and virtual functions in awhile so I am a bit rusty here.
(This assuming that the function is not a member function of the classes).
You will need to pass them to the function as a reference
or as a pointer;
and it should just plain call the correct version of the virtual method.
If you pass them without either of those
The object will be copy constructed to always be a Class1 in the function, and will always call Class1::func1()