Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Container with heterogenous pointer to data member.

Reply
Thread Tools

Container with heterogenous pointer to data member.

 
 
Vikas
Guest
Posts: n/a
 
      09-10-2004
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
 
Reply With Quote
 
 
 
 
Jonathan Mcdougall
Guest
Posts: n/a
 
      09-11-2004
> 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
 
Reply With Quote
 
 
 
 
David Hilsee
Guest
Posts: n/a
 
      09-11-2004
"Jonathan Mcdougall" <(E-Mail Removed)> wrote in message
news:C3y0d.46614$(E-Mail Removed).. .
> > 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


 
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
heterogenous container class A C++ 12 01-14-2011 03:18 AM
Bitfields in a heterogenous environment GalenTX C Programming 6 02-06-2005 09:29 PM
std::transform container => std::abs(container) Steven T. Hatton C++ 4 12-05-2004 07:10 AM
STL: container's values setup by another container Maitre Bart C++ 2 02-11-2004 12:11 AM
std::container::iterator vs std::container::pointer Vivi Orunitia C++ 11 02-04-2004 08:09 AM



Advertisments