Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Polimorphe Vector

Reply
Thread Tools

Polimorphe Vector

 
 
Marcelo
Guest
Posts: n/a
 
      12-14-2005
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
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      12-14-2005
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)
 
Reply With Quote
 
 
 
 
Zara
Guest
Posts: n/a
 
      12-14-2005
On Wed, 14 Dec 2005 10:57:56 +0100, Marcelo <(E-Mail Removed)>
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
 
Reply With Quote
 
Ingo Menger
Guest
Posts: n/a
 
      12-14-2005

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

 
Reply With Quote
 
Chuck F.
Guest
Posts: n/a
 
      12-14-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
how the vector is created, how to pass vector to webservices method apachesoap:Vector Rushikesh Joshi Perl Misc 0 07-10-2004 01:04 PM
how do i create a vector within a vector ? learningjava Java 5 10-17-2003 10:19 PM



Advertisments