我们在C ++中是否有等效的对象实现!!! [英] Does we have equivalent Object implementation in c++!!!
本文介绍了我们在C ++中是否有等效的对象实现!!!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我在Java Map< string,object>中有一个实现.
对象:类对象是类层次结构的根.每个类都有Object作为超类.所有对象(包括数组)都实现此类的方法.
我们在C ++中是否具有相同的实现方式,或者有人可以帮助我实现这一目标吗?
谢谢,
Mahesh
解决方案
不,您在C ++中没有这样的东西.但是,这很少需要.在大多数情况下,将完全不相关的对象存储在同一容器中没有任何意义.通常,所有类都继承自同一个基类,在这种情况下,您将基类用作所包含的类型.
您能详细解释一下您的问题到底是什么吗? "http://www.faqs.org/docs/think_java/TIJ303.htm#Heading1129">单根的层次结构" [书).
但是,正如Eckel指出的那样,您可以构建自己的单根类层次结构.为void *或更佳的情况是shared_ptr< void>.但是,如果您将不以某种方式关联的对象存储在同一集合中,则可能意味着您在某个地方缺少虚函数,并且过于依赖反射(C ++并非真正为之构建的) .
干杯,
灰
Hi All,
I have a implementation in Java Map<string,object>.
Object: Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.
Do we have same kind of implementation in C++ or anyone help me how to achieve this?
Thanks,
Mahesh
解决方案
No, you don''t have something like this in C++. However, this is very rarely needed. Most of the time, it doesn''t make any sense to store objects completely unrelated in the same container. Usually, all your classes inherit from a same base class, in which case you use the base class as type contained.
Could you explain a bit more in details what you problem is exaclty ?
C++
, unlikeJava
has not a single-rooted hierarchy (see, for instance, "The singly rooted hierarchy"[^] in Eckel''s "Thinking in Java" book).
However, as Eckel points out, you may build your own single-rooted class hierarchy.
The nearest thing to a common base class for everything (or rather a reference to a common base class) is void * or better still a shared_ptr<void>. However if you''re storing objects that aren''t related in some way in the same collection it probably means you''re missing a virtual function somewhere and leaning too much on reflection (which C++ isn''t really built for).
Cheers,
Ash
这篇关于我们在C ++中是否有等效的对象实现!!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文