我正在玩弄以下使用 std::shared_ptr (C++11) 的类结构:
#include <string>
#include <iostream>
#include <vector>
#include <memory>
//class Member is given by a 3rd party library, CAN'T modify its structure!
class Member {//no instance of this class allowed
public:
Member() {};//should never be called
virtual ~Member() = 0;//pure virtual distructor;
virtual void foo() {
std::cout<<"Member"<<std::endl;
}
};
Member::~Member() {} //need to 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 to 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 to use unique functions of ChildBase;
cb->var->foo2(); //can access directly as well;
//cb->var = std::make_shared<ChildMember>();
func(cb);
}
我尝试做的是设计两个类 (Base
和ChildBase
),其中每个类都有自己的成员Member
/ ChildMember
。在充分使用ChildBase
object 及其类型的成员对象之后,ChildMember
我将它传递给一个函数,该函数func
接收Base
并应该使用多态性调用成员的属性。foo
var
问题: (1) 在上面的变体中,编译器抱怨:
overriding 'virtual std::shared_ptr<Member> Base::get_var()'
get_var() {return var;}`
(2)
如果我注释掉 的ChildBase
实现get_var
,则该函数被视为Base
并因此返回一个Member
不具有的指针foo2
。
(3)
我也可以注释掉cb->get_var()->foo2()
。整个事情都编译了,但它似乎没有调用ChildMember::foo()
,因为没有输出Child Member
;
有些东西(希望)有点混乱,但我不明白是什么。请各位大神帮忙纠正一下好吗?
编辑1:
基于 Abhijit Kadam 的回答,以下代码完全编译:
#include <string>
#include <iostream>
#include <vector>
#include <memory>
class Member {//no instance of this class allowed
public:
Member() {};
virtual ~Member() = 0;//pure virtual distructor;
virtual void foo() {
std::cout<<"Member"<<std::endl;
}
};
Member::~Member() {} //need to define destructor for 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) {
for (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 在下面添加了一个答案。