I'm developing some container class, which has similar idea: holding pointer inside.
#include <iostream>
template<class T>
class Container
{
public:
Container ( )
{
pointer = new T ( );
}
~Container ( )
{
delete pointer;
}
T* operator->( )
{
return pointer;
}
private:
T* pointer;
};
struct Base
{
virtual void who ( )
{
std::cout << "Base" << std::endl;
}
};
struct Child : public Base
{
virtual void who ( ) override
{
std::cout << "Child" << std::endl;
}
};
void test ( Container<Base>& c )
{
c->who ( );
}
int main ( )
{
Container<Child> child;
test ( child );
}
This code fails to compile: error C2664: 'void test(Container &)' : cannot convert argument 1 from 'Container' to 'Container &'
However if I use std::shared_ptr, instead of Container, everything works fine. So question is:
Is std::shared_ptr-like polymorphysm implementable? Or is this feature, which was somehow harcoded in C++? Sorry for my primitive language.
Thank you!
Aucun commentaire:
Enregistrer un commentaire