这是什么?相当于泛型?
假设我正在实现一个多态树数据结构,它可以接受任何类型的数据...
Tree<Int> or Tree<String> or Tree<Object>
但我在 C++ 中实现它...我如何指定树可以包含类似于 Java 中的泛型的任意类型
在C++中是否也有一个相当于Java的Object对象,其中C++中的所有对象都继承Object
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Java 泛型最接近的等价物是 C++ 中的模板。严格来说,它并不等同。但这就是 C++ 中的内容。因此,为了满足您的需求,您必须做一些工作,以便可以用 C++ 编写等效的代码。
以下是一些比较 C++ 模板与 Java 泛型的文章的链接:
The nearest equivalent of Java's generic is template in C++. Its not equivalent as such, if strictly speaking. But that is what you've in C++. So to suit your need, you've to work a little bit so that you can write equivalent code in C++.
Here're few links to some articles that compare C++ template with Java generics:
如果您的目标是拥有一个可以包含任何内容的容器,则可以将容器的值类型设置为 提升任何内容。但听起来您正在寻找的是同质容器,并且此处的模板答案对此有意义。
If your goal is to have a single container that can contain anything, you could make the container's value type be Boost Any. But it sounds like what you're looking for is a homogenous container, and the template answers here make sense for that.
在 C++ 中,您使用模板,其形式
与泛型不同,它们提供了更多的灵活性(以及更多搬起石头砸自己脚的能力)。例如,您不能将模板类型参数限制为某种类型的子类。而且 C++ 在模板内部没有强类型检查,这很容易造成混乱。
In C++, you use templates, in the form
Templates aren't identical to generics, they offer a bit more flexibility (and more ability to shoot yourself in the foot). You can't constrain a template type parameter to be a subclass of a certain type for example. And C++ doesn't have strong type checking inside the template, which is where it can be easy to make a mess.
您正在寻找的是 C++ 模板。
您可以声明一个模板类,如下所示:
类并不像 C# 和 Java 等语言那样从 C++ 中的公共基础派生。您将得到的最接近的是
void*
,但这会将所有类型安全性排除在外,我不会推荐它。What you're looking for are C++ templates.
You can declare a templated class like:
Classes do not derive from a common base in C++, like they do in languages like C# and Java. The closest you'll get is a
void*
, but that throws all type-safety out the door and I wouldn't recommend it.您正在使用泛型语言:
Tree
或Tree
不应该保存任何类型的对象;它们专门保存整数或字符串。只有Tree
使用 C++ 模板来保存特定类型:
用法:
You are using the language of generics:
Tree<Int>
orTree<String>
aren't supposed to hold objects of any type; they specifically hold integers or strings. OnlyTree<Object>
would be able to hold "any" type, but C++ doesn't have a concept equivalent to "object". You might be able to hold "any" type in your tree using Boost.Any although I haven't used it myself.Use C++ templates to hold a specific type:
Usage:
模板。
http://www.cplusplus.com/doc/tutorial/templates/
Templates.
http://www.cplusplus.com/doc/tutorial/templates/
好吧,对 java 泛型的天真尝试将是
Tree
。这不是很好,很多人会用以下内容替换它:然后使用
Tree
。这使您可以指定哪些类型与其兼容。它相当于java的Object东西。另一个层是有
Tree n;
只需简单的 struct Node { TreeBase *b; 但是既然我们谈论的是树,C++ 有更好的方法来做到这一点,因为它是通过 C++ 类直接支持的:
一旦必要的类型可用,创建树就再简单不过了:
这种方法具有优点是每个树节点可以有不同的行为。缺点是它有点冗长。
Ok, naive attempt at java's generics would be
Tree<void*>
. That's not very good and many people will replace that with the following:And then use
Tree<TreeBase*>
. This lets you specify which types will be compatible with it. It's equivalent of the java's Object thing.Yet another layer would be to have
Tree<Node> n;
with just simplestruct Node { TreeBase *b; };
But since we're talking about trees, c++ has considerably better way to do that, since it's directly supported via the c++ classes:
Creating the tree couldnt be easier, once necessary types are available:
This approach has the advantage that every treenode can have different behaviour. The downside is that it's slightly verbose.