代码之家  ›  专栏  ›  技术社区  ›  Chris

在派生类构造函数中使用智能指针

  •  0
  • Chris  · 技术社区  · 8 年前

    Widget ,这样我就有了一个向量(shared?)基类中的指针,在转换和取消引用指针时可以访问原始对象(因此它们仍然是派生类)?



    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 类构造函数 小装置 设置如下:

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

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

    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 对象不引用内存中的任何有效位置,并失败,

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



    最终,我要做的是 只需保留以基类共享指针形式存在的各种派生类型的组件列表。

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

    2 回复  |  直到 8 年前
        1
  •  2
  •   Charles    8 年前

    正如我在评论中所建议的,也许 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){}
    }
    

    您可以将指针投射到 Component Component*

    那么,也许定义一个 virtual void Component::display() = 0 迫使继承人根据您的需要定义某种行为。

        2
  •  1
  •   Öö Tiib    8 年前

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

    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;
    }