How to: Create Umbrella Type
Hey there,
I was wondering if you know way to create an "umbrella" type/class/struct of existing c++ types or windows types.
Let me explain.
I want to address ~3 different structs with a single pointer and downcast but, they are not a part of the same hierarchy.
Is there a way that I could for example's sake, include a POINT and a RECT under some "umbrella" type so I can go like this:
1 2 3 4 5 6 7 8 9 10 11
|
// Working Variables
//---------------------------------------------------------
Umbrella* pUmbType1(NULL);
Umbrella* pUmbType2(NULL);
POINT ptExamp = { 0 };
RECT egRect = { 0, 0, 0, 0 };
// Typecast POINT and RECT to umbrella pointer
//---------------------------------------------------------
pUmbType1 = (Umbrella*)ptExamp;
pUmbType2 = (Umbrella*)egRect;
|
I want to do this, so I don't have to duplicate code for each type.
Thank you very much.
ugh...
boost::any doesn't have any pointer or reference typecasts -_-;;
Wrap references.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#include <iostream>
#include <boost/any.hpp>
#include <functional>
int main()
{
int i = 7 ;
double d = 56.7 ;
boost::any many[] { std::ref(i), &d } ;
int& r = boost::any_cast< std::reference_wrapper<int> >( many[0] ) ;
int* pi = &r ;
r = 789 ;
*pi += 32 ;
std::cout << i << '\n' ;
double* p = boost::any_cast<double*>( many[1] ) ;
*p = 0.1 ;
std::cout << d << '\n' ;
}
|
http://coliru.stacked-crooked.com/a/fd4709564e41871a
Topic archived. No new replies allowed.