How can I pass a class derived from an interface to a function taking the interface as a parameter?
I have an interface and a class set up something like this.
class Interface { public: virtual ~Interface() {} virtual void DoStuff() = 0; }; class MyClass : public Interface { public: MyClass(); ~MyClass(); void DoStuff() override; }; void TakeAnInterface(std::shared_ptr<Interface> interface); int main() { auto myInterface = std::make_shared<MyClass>(); TakeAnInterface(myInterface); } The compiler complains about No matching function call to TakeAnInterface(std::shared_ptr<MyClass>&). Why doesn't function TakeAnInterface recieve the Interface class instead of MyClass?
MyClasstoInterfaceis automatic, while there is no obvious cast fromstd::shared_ptr<MyClass>tostd::shared_ptr<Interface>