继承和指针
Inheritance and pointers
我有这样的代码:
class Human
{
protected:
int age;
std::string sex;
public:
virtual void speak() = 0;
};
class Child:public Human
{
public:
void speak(){std::cout << "I am Childn";}
};
class Man:public Human
{
public:
void speak(){std::cout << "I am Mann";}
};
class Woman:public Human
{
public:
void speak(){std::cout << "I am Womann";}
};
(don't know, std::shared_ptr<Human> maybe?) operator*(std::shared_ptr<Child> &b, int x)
{
b->setAge(b->getAge()+x);
if(b->getAge()>18 && b->getSex()=="Man")
{
return (i want b to become std::shared_ptr<Man>)
}
if(b->getAge()>18 && b->getSex()=="Woman")
{
return (here I want b to become std::shared_ptr<Woman>);
}
return;
}
int main(){
auto x = std::make_shared<Child>;
x*19;
}
我知道这似乎很奇怪,但这是我能想到的最简单的情况,而不必写下我在RN中挣扎的所有代码。有人可以解释什么类型应该是哪种类型,以及如何更改共享_ptr类型,知道他们是从同一父母派生的?
对象无法更改类型。Child
对象始终是Child
对象。您可以做的是使用所需的属性创建一个新的对象,然后返回:
std::shared_ptr<Human> operator*(std::shared_ptr<Human> b, int x)
{
b->setAge(b->getAge()+x);
if(b->getAge()>18 && b->getSex()=="Man") {
return std::make_shared<Man>(b->getAge());
} else if(b->getAge()>18 && b->getSex()=="Woman") {
return std::make_shared<Woman>(b->getAge());
} else {
return b;
}
}
int main(){
std::shared_ptr<Human> x = std::make_shared<Child>;
x = x*19;
}
这似乎不是一个好设计。Human
的儿童或成人状态将更好地表示为对象的属性,或者通过检查age
是否大于18的函数。
您无法使T<Derived>
类型从T<Base>
继承,因为C 模板不支持协方差。这样做对于某些类型来说将是不安全的,例如对容器的可变引用。(想象一下将std::vector<Cat>
提及为std::vector<Animal>&
并推回狗!)
(我会给这个答案作为评论,但我没有评论能力。)
更新:您可以编写处理堆数据的非模板包装器:
class Wrapper
{
public:
Wrapper(Base* b) : raw(b) {}
~Wrapper() { delete raw; }
Base& get() { return *base; }
private:
Base* raw;
}
当然,在您的示例中,您使用std :: shared_ptr,而不是std :: unique_ptr。您必须处理参考计数,而不是简单地删除毁灭仪中的数据,但是保持内部原始指针的技术仍然存在。
更新2:
可以使用上述代码提供一定的间接水平,以便所有从基类继承的类都可以以相同类型保存,而无需编写自己的参考计数器:
std::shared_ptr<Wrapper>
此解决方案可能被视为类似于执行std::shared_ptr<Base*>
,除了后一个解决方案会泄漏内存。
相关文章:
- 关于C++中具有多重继承"this"指针的说明
- 共享指针继承,而不先显式强制转换
- 派生类是从基类继承 v 指针并仅使用它,还是也有自己的 v 指针?
- 使用智能指针附加的继承对象的深层复制
- 嵌套类、继承和C++中的共享指针
- 删除C++中的继承类指针数组
- 具有虚拟指针和继承的 CPP 类大小
- 钻石继承虚拟成员铸造与指针
- 当键是虚拟继承中涉及的基类指针时,对 std::unordered_map 项的访问崩溃
- 当依赖关系和依赖关系都是多态时,在哪个继承级别存储依赖关系指针?
- cppyy 继承包含智能指针的类
- 作为模板参数的成员函数指针在继承的成员函数上失败,如何以及为什么?
- 是否可以将成员作为指针继承
- C++14 不能调用从唯一指针继承的类的复制构造函数或运算符 =
- 是无限的Typedef函数指针继承
- 是否可以从 C++11 智能指针继承并覆盖相关运算符?
- 继承:使用从父指针继承的方法
- 使用智能指针继承的皮条
- 是覆盖返回类型所需的指针-继承
- 调用未从BaseClass指针继承的DerivedClass函数