Polimorphe Vector

M

Marcelo

Hello,

I would like to know if it is possible to implement in C the Vector class of
java. I would like to have and object (or something like) that makes this:

Let's call v the vector:

v[0] is a String
v[1] is an int
v[2] is an array.
....etc ...

I would like to know if there is another possibility than creating a struct.

In java, there is a vector implementation for this (with an iterator variable).
Besides, java permits to test the type of the object. Is it possible to do it in
C (having something like "instanceof") ??

thank you very much for your help,

Marcelo
 
R

Richard Heathfield

Marcelo said:
Hello,

I would like to know if it is possible to implement in C the Vector class
of java. I would like to have and object (or something like) that makes
this:

Let's call v the vector:

v[0] is a String
v[1] is an int
v[2] is an array.
...etc ...

I would like to know if there is another possibility than creating a
struct.

Yes, this is possible, but only using C constructs that you probably already
know about - arrays, pointers, etc. It's a LOT of work, but can be worth it
if you really, really need dynamic typing.
In java, there is a vector implementation for this (with an iterator
variable). Besides, java permits to test the type of the object. Is it
possible to do it in C (having something like "instanceof") ??

As I said, it *is* possible - but there is no C syntax that is explicitly
designed to support it.
 
Z

Zara

Hello,

I would like to know if it is possible to implement in C the Vector class of
java. I would like to have and object (or something like) that makes this:

Let's call v the vector:

v[0] is a String
v[1] is an int
v[2] is an array.
...etc ...

I would like to know if there is another possibility than creating a struct.

In java, there is a vector implementation for this (with an iterator variable).
Besides, java permits to test the type of the object. Is it possible to do it in
C (having something like "instanceof") ??

thank you very much for your help,

Marcelo


Try using std::vector<boost:variant> or std::vector<boost::any>

www.boost.org

It is not STD C++, but it might be in a near future

Best regards,

-- Zara
 
I

Ingo Menger

Marcelo said:
Hello,

I would like to know if it is possible to implement in C the Vector class of
java. I would like to have and object (or something like) that makes this:

Let's call v the vector:

v[0] is a String
v[1] is an int
v[2] is an array.
...etc ...

This is not the way the class Vector is to be used in Java. As of Java
1.5, the javadoc tells us the following:

public class Vector<E> extends AbstractList<E> implements List<E>

That means, that all elements of the vector should be of one and the
same type, symbolized by E.

If you want just a dynamic array then pointers, malloc(), realloc() and
free() are your friends.

Besides, java permits to test the type of the object. Is it possible to do it in
C (having something like "instanceof") ??

No.
But you can implement something like it with a union. For example:

enum Kind { String, Int, Whatever };

struct Data {
enum Kind kind;
union {
char *strval;
int intval;
...
} u;
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top