gpt4 book ai didi

c++ - 将参数限制为 C++ 中的特定类和派生类

转载 作者:行者123 更新时间:2023-11-28 00:43:29 27 4
gpt4 key购买 nike

我正在从事一个项目,将一些代码从称为 MODSIM 的模拟语言转换为 C++,并且遇到了一些我难以理解的事情。

代码示例如下:

PlaneQueueObj = OBJECT(QueueObj[ANYOBJ:PlaneObj])

这意味着我们正在声明一个继承自 QueueObjclass PlaneQueueObj,它只允许 class PlaneObj 或类的对象从 PlaneObj 派生到队列中。 (类在这种语言中被称为对象...)

我想知道这如何在 C++ 中实现。
这可能真的很明显,我只是让自己感到困惑,但我是新来的,所以放轻松。

最佳答案

我认为您正在寻找的很简单,std::queue<std::unqiue_ptr<PlaneObj>> .

std::queue<std::unique_ptr<PlaneObj>> queue;

queue.push(new PlaneObjType1());
queue.push(new PlaneObjType2());
queue.push(new PlaneObjType3());

关于c++ - 将参数限制为 C++ 中的特定类和派生类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17546761/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com