473,320 Members | 2,158 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,320 software developers and data experts.

Container with heterogenous pointer to data member.

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
Jul 22 '05 #1
2 1162
> 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
Jul 22 '05 #2
"Jonathan Mcdougall" <jo***************@DELyahoo.ca> wrote in message
news:C3********************@wagner.videotron.net.. .
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
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: flacco | last post by:
is there a way to iterate over the *values* in a list/dict/whatever, regardless of whether it's a list, dict, or whatever? ie, the iteration code will not know beforehand what kind of container...
11
by: Vivi Orunitia | last post by:
Hi all, I tried looking this up in the sgi docs but it didn't provide any concrete answer to what I'm looking for. Basically, is there any difference between using ::iterator for a container vs...
3
by: Jazzkt | last post by:
I wrote a little code using the map container available in the STL of C++. The trouble I am having is that the find() member function is working properly. When I try to find a key, if that key is...
9
by: axel22 | last post by:
Hello, I have the following problem. I create a class called MyClass which includes another class calles MyContainer. Class MyContainer has a member which is a vector, instantiated as...
3
by: toton | last post by:
Hi, I have a container class, and I want to iterate over a portion of the container class while I insert/remove item from it. Noting down the present location & constructing iterator from there is...
20
by: Martin Jørgensen | last post by:
Hi, I'm reading a number of double values from a file. It's a 2D-array: 1 2 3 4 5 6 7 ------------- 1 3.2 2 0 2.1 3 9.3 4
18
by: Goran | last post by:
Hi @ all! Again one small question due to my shakiness of what to use... What is better / smarter? private: vector<MyClass_t* itsVector; OR...
7
by: ademirzanetti | last post by:
Hi there !!! I would like to listen your opinions about inherit from a STL class like list. For example, do you think it is a good approach if I inherit from list to create something like...
36
by: Peter Olcott | last post by:
So far the only way that I found to do this was by making a single global instance of the container class and providing access to the contained class, through this single global instance. Are...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.