[英]How to make a raw C++ pointer to point to a template class without specifying the type?
I have seen this in a large codebase, in a header file.我在 header 文件的大型代码库中看到了这一点。 When I tried to do it in a source file the compiler complained.当我尝试在源文件中执行此操作时,编译器抱怨。 The fact that it was OK for the compiler for it being in the header file was still amusing, so I'd like to know how and why it was working.编译器在 header 文件中可以接受这一事实仍然很有趣,所以我想知道它是如何工作的以及为什么工作。 It was a very complex class so I couldn't decipher it.这是一个非常复杂的 class 所以我无法破译它。
So here is a simple class:所以这里是一个简单的 class:
template <typename T>
class Container {
public:
Container(T value) : _value(value){};
T get() { return _value; }
void set(T value) { _value = value; }
private:
T _value;
};
Now in some other header file, I have this other class:现在在其他一些 header 文件中,我有这个其他 class:
class ContainerFactory
{
private:
Containter<int> *_container1{nullptr}; //this will work as expected
Container *_container2{nullptr}; //this won't work, the compiler will ask for a type
std::map<Container*, UIControl*> _map; //this won't work either, but it is handy
//if I have several types of Container.
}
What so called "tricks" do I have to do in order for _container2
declaration to compiler.为了将_container2
声明到编译器,我必须做什么所谓的“技巧”。 I tried a forward declaration and it didn't work obviously.我尝试了前向声明,但它显然不起作用。 Should I make another class that derives from Container
and do some additional stuff?我是否应该制作另一个源自Container
的 class 并做一些额外的事情? Please advise.请指教。 Thanks!谢谢!
A template is a construct which generates C++ entities (classes, functions, and variables at present).模板是生成 C++ 实体(目前是类、函数和变量)的构造。 But a template by itself is not the thing it generates;但是模板本身并不是它生成的东西。 it's just a pattern for generating things.它只是一种生成事物的模式。 It's a fiction that exists at compile time, not a thing that lives in memory somewhere.这是在编译时存在的虚构,而不是存在于 memory 某处的东西。
So you cannot have a pointer to a template, nor can you have an object of a template.因此,您不能拥有指向模板的指针,也不能拥有模板的 object。
You can accomplish the broad idea of what you want, an object that could refer to any instantiation of some template.您可以完成您想要的大致概念,一个 object 可以引用某个模板的任何实例化。 But this would involve creating a type-erased object which provides a specific interface for accessing the type-erased object.但这将涉及创建一个类型擦除的 object,它提供了一个用于访问类型擦除的 object 的特定接口。
However, type erasure is usually employed when the type you're trying to deal with cannot be named for various reasons or needs to be flexible.但是,当您尝试处理的类型由于各种原因无法命名或需要灵活时,通常会使用类型擦除。 If your type is a concrete type, then it must have some idea of what data it stores;如果您的类型是具体类型,那么它必须知道它存储了哪些数据; if it didn't, it wouldn't be able to use get
or set
(since those have to return a known type).如果没有,它将无法使用get
或set
(因为它们必须返回已知类型)。 So either your type needs to be a template (and thus the user defines which Container<T>
it works with), or the nature of the type needs to define the Container
's template parameter.所以要么你的类型需要是一个模板(因此用户定义了它使用的Container<T>
),或者类型的性质需要定义Container
的模板参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.