Polimorphe Vector

Discussion in 'C Programming' started by Marcelo, Dec 14, 2005.

  1. Marcelo

    Marcelo Guest

    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
     
    Marcelo, Dec 14, 2005
    #1
    1. Advertising

  2. 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)
     
    Richard Heathfield, Dec 14, 2005
    #2
    1. Advertising

  3. Marcelo

    Zara Guest

    On Wed, 14 Dec 2005 10:57:56 +0100, Marcelo <>
    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
     
    Zara, Dec 14, 2005
    #3
  4. Marcelo

    Ingo Menger Guest

    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;
    };
     
    Ingo Menger, Dec 14, 2005
    #4
  5. Marcelo

    Chuck F. Guest

    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/archives/2005/11/sonys_drm_rootk.html
     
    Chuck F., Dec 14, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. learningjava
    Replies:
    5
    Views:
    2,337
    Miguel De Anda
    Oct 17, 2003
  2. pmatos
    Replies:
    6
    Views:
    24,133
  3. Replies:
    8
    Views:
    2,003
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    626
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    399
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page