gpt4 book ai didi

c++ - 当传递对实现接口(interface)的类的引用时,优先选择重载函数

转载 作者:太空狗 更新时间:2023-10-29 23:10:45 24 4
gpt4 key购买 nike

这就是我正在使用的(大致)

template<class t>
class Manager{
//this class has a internal map which you can use to store classes of 1 type with a key.
//This class is also a templated singelton (which means that you can call getInstance() for
//any type like so: Manager<someType>::getInstance()
//...
public:
void addComponent(int key, const t& newThing){
//add to internal map
}

void addComponent(int key, AddsFriends<t>& newThing){
//add newThing & Friends to map
}
}

template<class t>
class AddsFriends<t>{
public:
virtual void addFriends(int key){
//add the Friend classes to the maps upon being called
}
}

这是我想出的一个练习,所以它并不太合乎逻辑。基本上,当我从任何类型获得一个管理器时,我应该能够使用指定的键和组件向它添加一个条目(它将在插入时复制)。我还可以使用 get 函数检索插入的类(上面未显示) .

当使用扩展“AddsFriends”的类型时,我希望能够使用键调用“addFriends”,以便可以将任何“Friend-instances”添加到许多类型。这样我有点想做一些东西,我可以有一个名为“Person”的类,并让它在添加人员时向具有相同 ID 的相应管理器添加一个“Hat”类(因此插入的每个“人”也会导致“帽子”被插入)。我希望这是可以理解的。

但是,我无法确定此接口(interface)是否已实现。我尝试执行 dynamic_cast 并只检查异常,但这需要丢失 const-expression -> 使插入语句更长(因为我必须“只是”存储变量以使其充当初始化程序而不是仅仅调用添加函数中的构造函数)

我怎样才能让 C++ 在这里选择正确的函数?

编辑:

以下是我打算如何使用管理器:

int key;
Manager<std::string>& stringManager = Manager<std::string>::getInstance();
stringManager.addComponent(key, "Hello there");

Manager<Foo>& fooManager = Manager<Foo>::getInstance();
fooManager.addComponent(key, Foo("Some init Params"));

class Foo: public AddsFriends<Foo>{
private:
std::string *friendString = nullptr;
//Other stuff
public:
//Do constructor and whatever else you want
virtual void addFriends(int key){
//add the Friend classes to the maps upon being called
Manager<std::string>& stringManager = Manager<std::string>::getInstance();
stringManager.addComponent(key, "This belongs to the foo component!");
//Note: The way I do this changed in my actual implementation, where I return a reference directly in the addComponent method. When I asked this question I was still using this function just because I didnt want to make the problem more complicated before I could get the old version to work. getNewest just return the reference to the newest component.
this->friendString = &stringManager.getNewest();

}

}

我希望以这种方式添加 friend 的原因是,我可以拥有通过组合使用与其他组件相同功能的“组件”,但仍然可以通过适当的管理器访问它们使用的“组件”。假设我有一个名为“Cube”的组件和一个名为“Position”的组件。多维数据集包含表示多维数据集所需的数据,但它也分配了一个位置。如果我只是使用“Position”属性进行常规合成,它会起作用,但 Position 不会像它应该的那样位于 PositionManager 中。这将导致一组不完整的托管组件,并且有点破坏了将这些管理器放在首位的意义:/。

最佳答案

我认为最简单的选择是专门化 Manager

template <class t>
class Manager {
public:
void addComponent(int key, const t& newThing){
//add to internal map
}
};

template <class t>
class Manager<AddsFriends<t>> {
public:
void addComponent(int key, AddsFriends<t> const& newThing){
//add newThing & Friends to map
}
};

关于c++ - 当传递对实现接口(interface)的类的引用时,优先选择重载函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55184672/

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