473,413 Members | 1,811 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

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
Dec 14 '05 #1
4 1187
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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: {AGUT2}=IWIK= | last post by:
Hello all, It's my fisrt post here and I am feeling a little stupid here, so go easy.. :) (Oh, and I've spent _hours_ searching...) I am desperately trying to read in an ASCII...
9
by: luigi | last post by:
Hi, I am trying to speed up the perfomance of stl vector by allocating/deallocating blocks of memory manually. one version of the code crashes when I try to free the memory. The other version...
7
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.