在派生类构造函数

时间:2017-07-11 22:01:11

标签: c++ oop inheritance polymorphism shared-ptr

问题,一开始:(前言:c ++ oop编程新手)

如何构造派生类Widget,这样我就可以在基类中使用(共享?)指针的向量,其中原始对象(这样它们仍然是派生类)可以在转换和取消引用指针时访问它吗?

说我有一个基类:

class Component {
public: 
    int a; 
    int b;
    virtual void rtti(){}; // for run time type information.
    explicit Component(int a, int b) { this->a = a; this->b = b;}
}

两个派生类,

class AComponent:public Component{
public:
    using Component::Component;
    AComponent(int a, int b) : Component(int a, int b){}
}    

class BComponent:public Component{
public:
    using Component::Component;
    BComponent(int a, int b) : Component(int a, int b){}
}

此外,我有一个多组件(这里仍然是通用的):

typedef shared_ptr<AComponent> AComponentPtr;
typedef shared_ptr<BComponent> BComponentPtr;
class MultiComponent{
public:
    vector<AComponentPtr> A_components;
    vector<BComponentPtr> B_components;

    explicit MultiComponent(vector<AComponentPtr> As, vector<BComponentPtr> Bs){
        this->A_components = As;
        this->B_components = Bs;
    }
}

最后,我有一个这个组件层次结构的特定用例:

class WidgetComponentA:public AComponent{...}
class WidgetComponentB:public BComponent{...}

class Widget:public MultiComponent{
public:
    using MultiComponent::MultiComponent;

    Widget(WidgetComponentA a, WidgetComponentB b, WidgetComponentB c)
        : MultiComponent(???){
    }
}

目前,我在MultiComponent中的Widget类构造函数设置如下:

class Widget:public MultiComponent{
public:
    using MultiComponent::MultiComponent;

    Widget(WidgetComponentA a, WidgetComponentB b, WidgetComponentB c)
        : MultiComponent({(AComponentPtr)&a},{(BComponentPtr)&b, (BComponentPtr)&c}){}
}

因为这不会在编译时产生错误。

然后,我在main方法中构建小部件,如下所示:

main(){

    WidgetComponentA a = WidgetComponentA(1,2);
    WidgetComponentB b = WidgetComponentB(3,4);
    WidgetComponentB c = WidgetComponentB(5,6);

    // now, the widget: 

    Widget widget = Widget(a,b,c);

    // however, the pointers within the widget 
    // do not access valid addresses in memory. 

return 0;}

Widget widget对象中的共享指针不引用内存中的任何有效位置,并且失败,

  

尝试获取不在内存中的值的地址。

最终,我要做的是让Widget以基类共享指针的形式保持各种派生类型的组件列表。

然后,我在类上运行泛型模板函数,并且仅在特定于小部件的函数中将指针强制转换为特定于小部件的派生类指针。

我使用共享指针是安全的,因为我遇到了内存泄漏警告......但是如果有更简单的解决方案......

2 个答案:

答案 0 :(得分:2)

正如我在评论中所建议的那样,或许polymorphic approach会更容易......

class MultiComponent{
  public:
    typedef std::vector<std::shared_ptr<Component>> components_vec;
    components_vec components;

    MultiComponent(components_vec& cv){
        components = cv;
    }
}

class Widget: public MultiComponent {
  public:

    Widget(MultiComponent::components_vec& cv)
        : MultiComponent(cv){}
}

您可以将指针投射到ComponentComponent*的后代,然后将它们存储在一起。

然后,或许定义一个virtual void Component::display() = 0来强制继承者根据您的需要定义某种行为。

答案 1 :(得分:1)

也许我误解了你的要求但是如果你想处理共享所有权的对象那么你必须这样做:

main()
{
    auto a = std::make_shared<WidgetComponentA>(1,2);
    auto b = std::make_shared<WidgetComponentB>(3,4);
    auto c = std::make_shared<WidgetComponentB>(5,6);

    // now, pass the shared stuff to widget: 

    Widget widget = Widget(a,b,c); // make sure that Widget has
                                   // such constructor that accepts
                                   // the shared pointers

    return 0;
}
相关问题