这是什么?相当于泛型?

发布于 2024-10-31 04:38:37 字数 234 浏览 1 评论 0 原文

假设我正在实现一个多态树数据结构,它可以接受任何类型的数据...

Tree<Int> or Tree<String> or Tree<Object>

但我在 C++ 中实现它...我如何指定树可以包含类似于 Java 中的泛型的任意类型

在C++中是否也有一个相当于Java的Object对象,其中C++中的所有对象都继承Object

suppose i'm implementing a polymorphic tree data structure that can take on data of any type...

Tree<Int> or Tree<String> or Tree<Object>

but I'm implementing it in C++....how would I specify that the tree can contain an arbitrary type similar to generics in Java

also is there an equivalent of Java's Object object in C++ in which all objects in C++ inherits Object

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

再见回来 2024-11-07 04:38:38

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:

睫毛溺水了 2024-11-07 04:38:38

如果您的目标是拥有一个可以包含任何内容的容器,则可以将容器的值类型设置为 提升任何内容。但听起来您正在寻找的是同质容器,并且此处的模板答案对此有意义。

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.

や莫失莫忘 2024-11-07 04:38:38

在 C++ 中,您使用模板,其形式

template <typename T>
    class Tree {
        TreeNode<T> Root
        ...
    };

与泛型不同,它们提供了更多的灵活性(以及更多搬起石头砸自己脚的能力)。例如,您不能将模板类型参数限制为某种类型的子类。而且 C++ 在模板内部没有强类型检查,这很容易造成混乱。

In C++, you use templates, in the form

template <typename T>
    class Tree {
        TreeNode<T> Root
        ...
    };

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.

我乃一代侩神 2024-11-07 04:38:38

您正在寻找的是 C++ 模板

您可以声明一个模板类,如下所示:

template<class TType>
class AClass
{
private:
    TType *somePointer;
};

类并不像 C# 和 Java 等语言那样从 C++ 中的公共基础派生。您将得到的最接近的是 void*,但这会将所有类型安全性排除在外,我不会推荐它。

What you're looking for are C++ templates.

You can declare a templated class like:

template<class TType>
class AClass
{
private:
    TType *somePointer;
};

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.

空心↖ 2024-11-07 04:38:38

您正在使用泛型语言: TreeTree 不应该保存任何类型的对象;它们专门保存整数或字符串。只有 Tree能够保存“任意”类型,但 C++ 没有相当于“对象”的概念。您可以使用 Boost 在树中保存“任何”类型.Any虽然我自己没用过。

使用 C++ 模板来保存特定类型:

template<typename T>
class Tree {
    ...
};

用法:

Tree<int> treeOfInts;

You are using the language of generics: Tree<Int> or Tree<String> aren't supposed to hold objects of any type; they specifically hold integers or strings. Only Tree<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:

template<typename T>
class Tree {
    ...
};

Usage:

Tree<int> treeOfInts;
萌能量女王 2024-11-07 04:38:38

好吧,对 java 泛型的天真尝试将是 Tree。这不是很好,很多人会用以下内容替换它:

class TreeBase { };
class TreeInt : public TreeBase { int i; };

然后使用 Tree。这使您可以指定哪些类型与其兼容。它相当于java的Object东西。

另一个层是有Tree n; 只需简单的 struct Node { TreeBase *b; 但是既然

我们谈论的是树,C++ 有更好的方法来做到这一点,因为它是通过 C++ 类直接支持的:

class TreeNode { virtual ~TreeNode() { } };
class MyIntTreeNode : public TreeNode {
public:
    MyTreeNode(int i) : i(i) { }
    int i;
};
class MyStringTreeNode : public TreeNode {
public:
   MyStringTreeNode(string s) : s(s) { }
   string s;
};
class MyArrayTreeNode : public TreeNode {
public:
   MyArrayTreeNode(std::vector<TreeNode*> v) : v(v) { }
   std::vector<TreeNode*> v;
};

一旦必要的类型可用,创建树就再简单不过了:

int main() {
  MyIntTreeNode n1(10);
  MyStringTreeNode n2("abc");
  std::vector<TreeNode*> v;
  v.push_back(&n1);
  v.push_back(&n2);
  MyArrayTreeNode n3(v);
  algo(n3);
  }

这种方法具有优点是每个树节点可以有不同的行为。缺点是它有点冗长。

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:

class TreeBase { };
class TreeInt : public TreeBase { int i; };

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 simple struct 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:

class TreeNode { virtual ~TreeNode() { } };
class MyIntTreeNode : public TreeNode {
public:
    MyTreeNode(int i) : i(i) { }
    int i;
};
class MyStringTreeNode : public TreeNode {
public:
   MyStringTreeNode(string s) : s(s) { }
   string s;
};
class MyArrayTreeNode : public TreeNode {
public:
   MyArrayTreeNode(std::vector<TreeNode*> v) : v(v) { }
   std::vector<TreeNode*> v;
};

Creating the tree couldnt be easier, once necessary types are available:

int main() {
  MyIntTreeNode n1(10);
  MyStringTreeNode n2("abc");
  std::vector<TreeNode*> v;
  v.push_back(&n1);
  v.push_back(&n2);
  MyArrayTreeNode n3(v);
  algo(n3);
  }

This approach has the advantage that every treenode can have different behaviour. The downside is that it's slightly verbose.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文