Container with heterogenous pointer to data member.

V

Vikas

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
 
J

Jonathan Mcdougall

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
 
D

David Hilsee

Jonathan Mcdougall said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top