c++ - inheritance and polymorphism for class and its member at the same time (shared_ptr, C++11) -
i playing around following structure of classes use std::shared_ptr (c++11):
#include <string> #include <iostream> #include <vector> #include <memory> //class member given 3rd party library, can't modify structure! class member {//no instance of class allowed public: member() {};//should never called virtual ~member() = 0;//pure virtual distructor; virtual void foo() { std::cout<<"member"<<std::endl; } }; member::~member() {} //need define destructor because of child classes class childmember : public member { public: childmember() {}; virtual void foo() { std::cout<<"child member"<<std::endl; } virtual void foo2() { std::cout<<"unique foo in child"<<std::endl; } }; class base { public: base() {}; virtual std::shared_ptr< member > get_var() {return var;} virtual void set_var ( std::shared_ptr< member > v) { var = v;} std::shared_ptr< member > var; }; class childbase : public base { public: childbase() { //var = std::make_shared<childmember>(); }; virtual std::shared_ptr< childmember > get_var() {return var;} //(2) : try comment virtual void set_var ( std::shared_ptr< childmember > v) { var = v;} std::shared_ptr< childmember > var; }; void func(std::shared_ptr<base> b) { b->get_var()->foo();// process input using polymorphism } int main() { std::shared_ptr<childbase> cb( std::make_shared<childbase>()); cb->set_var (std::make_shared<childmember>() ); cb->get_var()->foo2();//(3) want use unique functions of childbase; cb->var->foo2(); //can access directly well; //cb->var = std::make_shared<childmember>(); func(cb); }
what try design 2 classes (base
, childbase
) each 1 has own member member
/ childmember
. after full usage of childbase
object , member object of type childmember
, pass function func
receives base
, should, using polymorphism call proper foo
of member var
.
problems: (1) in variant in above, compiler complains that:
overriding 'virtual std::shared_ptr<member> base::get_var()' get_var() {return var;}`
(2) if comment-out childbase
implementation of get_var
, function treated base
, returns pointer member
not have foo2
.
(3) can comment out cb->get_var()->foo2()
. whole things compiles, seems not call childmember::foo()
there no output child member
;
something (hopefully) messed don't see what. guys please correct it?
edit1:
based on abhijit kadam answer, following code compiles:
#include <string> #include <iostream> #include <vector> #include <memory> class member {//no instance of class allowed public: member() {}; virtual ~member() = 0;//pure virtual distructor; virtual void foo() { std::cout<<"member"<<std::endl; } }; member::~member() {} //need define destructor child classes class childmember : public member { public: childmember() {}; virtual void foo() { std::cout<<"child member"<<std::endl; } void foo2() { std::cout<<"unique foo in child"<<std::endl; } }; class base { public: base() {}; std::shared_ptr< member > get_var() {return var;} void set_var ( std::shared_ptr< member > v) { var = v;} std::shared_ptr< member > var; }; class childbase : public base { public: childbase() { //var = std::make_shared<childmember>(); }; std::shared_ptr< childmember > var; }; void func(std::shared_ptr<base> b) { b->get_var()->foo();// process input using polymorphism } void func_vec( std::vector< std::shared_ptr<base> > vec) { (unsigned int i=0;i<vec.size();i++) vec[i]->get_var()->foo(); } int main() { std::shared_ptr<childbase> cb( std::make_shared<childbase>()); cb->set_var (std::make_shared<childmember>() ); func(cb); std::vector< std::shared_ptr<base>> vec; vec.push_back(cb); func_vec(vec); cb->var->foo2(); /*std::shared_ptr<childmember> ptr(std::dynamic_pointer_cast<childmember>(cb->get_var()) ); if (ptr) { ptr->foo2(); } */ }
edit2 added answer below.
as return types of overriding functions cannot differ have use dynamic cast implement solution. hence no need have virtual functions `get_var , set_var'. no need foo2 virtual unless class further derived other class.
int main() { std::shared_ptr<childbase> cb( std::make_shared<childbase>()); cb->set_var (std::make_shared<childmember>() ); shared_ptr<childmember> ptr(dynamic_pointer_cast<childmember>(cb->get_var()) ); if (ptr) { ptr->foo2(); } } class member {//no instance of class allowed public: member() {}; virtual ~member() = 0;//pure virtual distructor; virtual void foo() { std::cout<<"member"<<std::endl; } }; member::~member() {} //need define destructor child classes class childmember : public member { public: childmember() {}; virtual void foo() { std::cout<<"child member"<<std::endl; } void foo2() { std::cout<<"unique foo in child"<<std::endl; } }; class base { public: base() {}; std::shared_ptr< member > get_var() {return var;} void set_var ( std::shared_ptr< member > v) { var = v;} std::shared_ptr< member > var; }; class childbase : public base { public: childbase() { //var = std::make_shared<childmember>(); }; std::shared_ptr< childmember > var; }; void func(std::shared_ptr<base> b) { b->get_var()->foo();// process input using polymorphism }
Comments
Post a Comment