By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,795 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

Polimorphe Vector

P: n/a
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
Dec 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Dec 14 '05 #2

P: n/a
On Wed, 14 Dec 2005 10:57:56 +0100, Marcelo <ma********@yahoo.com>
wrote:
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
Dec 14 '05 #3

P: n/a

Marcelo schrieb:
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;
};

Dec 14 '05 #4

P: n/a
Zara wrote:
.... snip ...
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


This is c.l.c. The C++ language is off-topic here.

--
Read about the Sony stealthware that is a security leak, phones
home, and is generally illegal in most parts of the world. Also
the apparent connivance of the various security software firms.
http://www.schneier.com/blog/archive...drm_rootk.html
Dec 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.