Container with heterogenous pointer to data member.

Discussion in 'C++' started by Vikas, Sep 10, 2004.

  1. Vikas

    Vikas Guest

    Hi y'all,

    Is it possible to have polymorphic type for pointer to data members?
    I am trying to implement a functionality where various heterogenous
    pointers to data members are stored in a containter.

    Following example illustrates my question.

    class foo
    {
    int a;
    double b;
    ...
    }

    Now I can do something like the following:
    vector<int foo::*> vector_containing_ptr_to_int_data_member_of_foo

    I want to have a container with multiple types
    vector<T foo::*> vector_containing_heterogenous_ptr_to_data_member_of_foo
    (T is for any type)

    Is it possible?

    Thanks, Vikas
    Vikas, Sep 10, 2004
    #1
    1. Advertising

  2. > I want to have a container with multiple types
    > vector<T foo::*> vector_containing_heterogenous_ptr_to_data_member_of_foo
    > (T is for any type)
    >
    > Is it possible?


    No, not directly. In C++, everything must have a type, you cannot
    specify a "variant" type or whatever. You have very few solutions, the
    best one I can think of is the polymorphic one :

    class Object
    {
    };

    class Integer : public Object
    {
    };

    class Double : public Object
    {
    };

    std::vector<Object*> objects;

    "But I could have an infinite number of different types!" Yes, in that
    case, write it in another language. C++ is very rigid on types.


    Jonathan
    Jonathan Mcdougall, Sep 11, 2004
    #2
    1. Advertising

  3. Vikas

    David Hilsee Guest

    "Jonathan Mcdougall" <> wrote in message
    news:C3y0d.46614$...
    > > I want to have a container with multiple types
    > > vector<T foo::*>

    vector_containing_heterogenous_ptr_to_data_member_of_foo
    > > (T is for any type)
    > >
    > > Is it possible?

    >
    > No, not directly. In C++, everything must have a type, you cannot
    > specify a "variant" type or whatever. You have very few solutions, the
    > best one I can think of is the polymorphic one :
    >
    > class Object
    > {
    > };
    >
    > class Integer : public Object
    > {
    > };
    >
    > class Double : public Object
    > {
    > };
    >
    > std::vector<Object*> objects;
    >
    > "But I could have an infinite number of different types!" Yes, in that
    > case, write it in another language. C++ is very rigid on types.


    To Vikas: The boost::any class can make the above code a little simpler and
    it will allow the std::vector to hold objects of types that are not
    descendants of the Object class.

    --
    David Hilsee
    David Hilsee, Sep 11, 2004
    #3
    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. Fraser Ross
    Replies:
    4
    Views:
    1,023
    Fraser Ross
    Aug 14, 2004
  2. GalenTX

    Bitfields in a heterogenous environment

    GalenTX, Feb 4, 2005, in forum: C Programming
    Replies:
    6
    Views:
    318
    GalenTX
    Feb 6, 2005
  3. A
    Replies:
    12
    Views:
    452
  4. Stephen Howe
    Replies:
    2
    Views:
    276
    Stephen Howe
    Nov 6, 2012
  5. somenath
    Replies:
    10
    Views:
    256
    James Kanze
    Jul 2, 2013
Loading...

Share This Page