简体   繁体   English

std::make_shared 接口?

[英]std::make_shared an interface?

I have a Component class who inherits from IComponent.我有一个继承自 IComponent 的组件 class。 This function link a component with antoher.此 function 将一个组件与另一个链接。

    void Component::link(IComponent &other)
    {
        components->setRef(std::make_shared<IComponent>(other));
    }

When I compile my program I have this error:当我编译我的程序时,我有这个错误:

error: invalid new-expression of abstract class type ‘IComponent’

How can I fix this?我怎样才能解决这个问题?

PS: I understand that I can't instantiate an Interface because she's contain pure virtual function, but I don't know how to avoid this problem PS:我知道我无法实例化一个接口,因为她包含纯虚拟 function,但我不知道如何避免这个问题

It's quite unclear what you trying to do here.很不清楚你在这里想做什么。 "setRef()" sounds like its argument is a reference, if so you don't need to do anything with (smart) pointers. “setRef()”听起来它的参数是一个引用,如果是这样,你不需要对(智能)指针做任何事情。 You should at least show the prototype of that method.您至少应该展示该方法的原型。

As noted above, if you want to "make a copy" of "other" you should do it by "cloning" interface, eg create virtual "IComponent* IComponent::clone()" method and implement it properly in Component.如上所述,如果您想“复制”“其他”,您应该通过“克隆”接口来完成,例如创建虚拟“IComponent* IComponent::clone()”方法并在组件中正确实现它。 Also if it's guaranteed "other" is a reference to Component that you can cast and copy-construct the new instance (but that's unlikely what your teacher wants you too do).此外,如果保证“其他”是对 Component 的引用,您可以强制转换和复制构建新实例(但这不太可能是您的老师希望您做的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM