模板类指针 c++宣言
template <typename T>
class Node
{...};
int main
{
Node* ptr;
ptr = new Node<int>;
}
将无法编译我必须将指针声明为
Node<int>* ptr;
为什么在声明指针时必须指定类型我还没有创建该类,为什么编译器必须知道它将指向什么类型。是否不可能创建一个通用指针并随后决定我要分配给它的类型。
template <typename T>
class Node
{...};
int main
{
Node* ptr;
ptr = new Node<int>;
}
Will fail to compile I have to to declare the pointer as
Node<int>* ptr;
Why do I have to specify the type when declaring a pointer I haven't created the class yet, why does the compiler have to know what type it will be pointing to. And is it not possible to create a generic pointer and decide afterwards what type I want to assign it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
模板化在编译时解析类型。当您将新的
Node
对象分配给它时,指针必须在编译时知道它到底是什么类型。Node
和Node
在二进制文件中可能非常不同(对象的二进制布局完全根据模板参数发生变化),因此它不会拥有指向模板的未解析的指针类型是没有任何意义的。您应该首先为节点定义一个公共父类:
Templating resolves types at compile-time. When you assign the new
Node<int>
object to it, the pointer must know at compile-time what type exactly it is.Node<int>
andNode<std::vector>
can be very different in the binaries (the binary layout of your object changes completely according the template parameter) so it doesn't make any sense to have an unresolved pointer type to a template.You should define first a common parent class for your nodes:
简单的答案是因为 C++ 使用(相当)严格的静态类型
检查。
Node
是与Node
完全无关的类型,当编译器看到
ptr->doSomething()
时,它必须知道是否调用
Node::doSomething()
或Node::doSomething()
。如果您确实需要某种动态通用性,那么
ptr
将指向的实际类型仅在运行时才知道,您需要定义一个基类,并从中派生。 (这是一个相当常见的
准确地说,类模板从非模板基派生的习惯用法
以便可以在运行时解决指针的通用性。)
The simple answer is because C++ uses (fairly) strict static type
checking.
Node<int>
is a completely unrelated type toNode<double>
,and when the compiler sees
ptr->doSomething()
, it has to know whetherto call
Node<int>::doSomething()
orNode<double>::doSomething()
.If you do need some sort of dynamic generality, where the
actual type
ptr
will point to will only be known at runtime, you needto define a base class, and derive from that. (It's a fairly common
idiom for a class template to derive from a non-template base, precisely
so that the generality in the pointers can be resolved at runtime.)
您可以使用指针做很多事情,仅列出极少数:
为了使编译器生成有效执行这些操作的代码,它需要知道指针的类型。如果它推迟决策直到看到指针的类型,那么它将需要:
有点......你有很多选择:
void*
,但在它再次对指向类型进行有意义的操作之前,你需要手动将其转换回该类型:您的情况意味着在某处记录它的内容,然后为每种可能性使用单独的代码boost::any<>
- 非常类似于void*
,但具有安全性 中boost::variant<>
- 更安全、更方便,但是在创建指针时必须列出可能的指向类型,Node
的指针,它声明了您用于在任何特定类型的节点上操作的共享函数和成员数据,然后是模板化的Node
类派生自抽象Node
并实现特定于类型的函数版本。然后使用虚拟函数通过指向基类的指针来调用它们。There are lots of things you're allowed to do with a pointer, just to list a very few:
For the compiler to generate code to do these efficiently, it needs to know the type of the pointer. If it defered the decisions until it saw the type of the pointer, then it would need to either:
Kind of... you have many options:
void*
, but before it can meaningfully operate on the pointed-to-type again you'll need to manually cast it back to that type: in your case that means recording somewhere what it was then having separate code for every possibilityboost::any<>
- pretty much like avoid*
, but with safety built inboost::variant<>
- much safer and more convenient, but you have to list the possible pointed-to types when you create the pointerNode
that declares the shared functions and member data you'd use to operate on any particular type of node, then the templatedNode
class derives from that abstractNode
and implements type-specific versions of the functions. These are then called via a pointer to the base class usingvirtual
functions.每当您在 C++ 中创建任何类型的对象(包括指针)时,都必须知道该对象的完整类型。您的代码中没有
Node
这样的类型,因此您无法创建指向它的指针实例。您需要重新思考如何设计和编写代码。Whenever you create any kind of object (including pointers) in C++, the full type of the object must be known. There is no such type in your code as
Node
, so you can't create instances of pointers to it. You need to rethink how you are designing and writing your code.正如 Neil Butterworth 和 Luc Danton 所指出的,您不能拥有 Node* 类型的指针,因为 Node 不是类型。它是一个模板。是的,Node 是一个类模板,但这里的 class 只是限定了模板的类型。一种看待它的方法是:正如类和类的实例是非常不同的东西一样,模板和模板实例化也是非常不同的。那些模板实例(例如 Node)就是类。类模板是另一种野兽。
As both Neil Butterworth and Luc Danton noted, you can't have a pointer of type Node* because Node is not a type. It is a template. Yes, Node is a class template, but that class here just qualifies the kind of template. One way to look at it: Just as classes and instances of classes are very different things, so too are templates and template instantiations. It is those template instantiations such as Node that are classes. The class template is some other kind of beast.