Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 12-Oct-10 20:16pm
v3

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 ?
 
Share this answer
 
Comments
MaheshPeddi 13-Oct-10 4:48am    
I have a Java Class consists of Map<string, object=""> where the key is string & Object is value!. Here Object can store any kind of variables either system defined or user defined . Now i want to migrate the same in C++. Below is the code snippet in Java.
public class GNode implements Serializable
{
private Map<string, object=""> GObj;
}
Cedric Moonen 13-Oct-10 4:57am    
Your code snippet is unreadable. Anyway, your design is probably very bad if you have to store any kind of object in the same container. What is its purpose ? What will you do with the contained objects ? What do you mean also by "system defined" ?
The important point here is that the objects in the container have to be used in some way. And you can't use an object if you don't know what it is (of course you can later check its type but still, I don't see clearly what you're going to do with that).
MaheshPeddi 13-Oct-10 5:28am    
I am migrating Java code to C++ in one of the class they are using member variable as Map<string,object>and I think he may store other class objects in that map.
Cedric Moonen 13-Oct-10 5:37am    
You still didn't answer my questions. I know that you are migrating Java to C++. But migrating doesn't mean blindy "translate" from java to C++. You have to understand what the program does and in this case what the program is doing with the objects in the map. So please, read my post once again carefully and try to answer my questions. The more precise you'll answer, the more chance you'll have to receive a precise answer.
C++, unlike Java 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.
 
Share this answer
 
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900