C ++和Java容器 [英] C++ and Java Containers

查看:55
本文介绍了C ++和Java容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我们有一个名为"ctr"的容器类,它可以容纳Obj类的任何子类的对象,并且我们需要一个新的容器来限制它.插入第一个对象后,插入的任何其他对象必须与第一个对象属于同一类(或子类),并且静默忽略任何违反此约束的尝试.

我想创建一个新的容器类,将其称为subCtr,它提供此功能并将对象内部存储在"ctr"容器中.

我正在实现的构造方法如下:

int addItem(Obj item)//添加一个项目,并返回一个唯一的ID,该ID可在以后用于检索该项目

Obj getItem(int i)//返回对项i的引用.调用者将在需要时手动投射项目,因此您无需进行此投射.

我正在尝试使用C ++或Java来实现.

非常感谢您的帮助.非常感谢.

Let''s say we have a container class named "ctr" that can hold objects of any subclass of the class Obj and we want a new container which limits this. Once the first object is inserted, any other object that is inserted must be of the same class (or subclass) as the first object, and silently ignoring any attempt to violate this constraint.

I want to create a new container class, let''s call it subCtr, that provides this functionality and internally stores objects in a "ctr" container.

The constructor methods I am implementing are as follows:

int addItem(Obj item) // adds an item, and returns a unique ID that can be used later to retrieve the item

Obj getItem(int i) // Return a reference to item i. The caller will manually cast the item if needed, so you are not required to do this cast.

I''m trying to do it in either C++ or Java.

Your help is much appreciated. Thanks much.

推荐答案

看看 Java泛型 [
Take a look at C++ templates[^] and Java generics[^]; they may be what you need.


这篇关于C ++和Java容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆