Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Polimorphe Vector (http://www.velocityreviews.com/forums/t440536-polimorphe-vector.html)

Marcelo 12-14-2005 09:57 AM

Polimorphe Vector
 
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

Richard Heathfield 12-14-2005 10:02 AM

Re: Polimorphe Vector
 
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)

Zara 12-14-2005 10:39 AM

Re: Polimorphe Vector
 
On Wed, 14 Dec 2005 10:57:56 +0100, Marcelo <mache_1999@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

Ingo Menger 12-14-2005 10:54 AM

Re: Polimorphe Vector
 

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;
};


Chuck F. 12-14-2005 12:46 PM

Re: Polimorphe Vector
 
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


All times are GMT. The time now is 08:23 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.