Connecting Tech Pros Worldwide Forums | Help | Site Map

Templates problem

Christopher
Guest
 
Posts: n/a
#1: Jul 22 '05
I would like to make a template data structure such that any type of
data structure may be contained within the template object with the
constraint that the object being contained must have a specific data
member of a specific type. Does there exist a c++ construct to
accomplish this task?

EX
template class container
{
methods
..
..
..
Data * list_of_data; // Instead of type Data, could be any type.
};

class Data // Mock data type
{
methods
..
..
Bounding_Box bounds; // Must have this!
};

Not sure if that got my point across :(

Thanks,
Christopher

Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Templates problem


Christopher wrote:[color=blue]
> I would like to make a template data structure such that any type of
> data structure may be contained within the template object with the
> constraint that the object being contained must have a specific data
> member of a specific type. Does there exist a c++ construct to
> accomplish this task?
>
> EX
> template class container
> {
> methods
> .
> .
> .
> Data * list_of_data; // Instead of type Data, could be any type.
> };
>
> class Data // Mock data type
> {
> methods
> .
> .
> Bounding_Box bounds; // Must have this!
> };
>
> Not sure if that got my point across :([/color]

// assume we did define Bounding_Box somehow

class BaseData {
public:
virtual ~BaseData() {}
Bounding_Box bounds;
};

class OtherClass : public BaseData {
...
};

class YetAnotherClass : public BaseData {
...
};

....
std::list<BaseData*> list_of_data;

And then fill 'list_of_data' with objects obtained by 'new OtherClass'
or 'new YetAnotherClass'.

Read about heterogeneous containers.

Victor
David Hilsee
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Templates problem


"Christopher" <cpisz@austin.rr.com> wrote in message
news:71dce74a.0409031030.6fcbcd89@posting.google.c om...[color=blue]
> I would like to make a template data structure such that any type of
> data structure may be contained within the template object with the
> constraint that the object being contained must have a specific data
> member of a specific type. Does there exist a c++ construct to
> accomplish this task?
>
> EX
> template class container
> {
> methods
> .
> .
> .
> Data * list_of_data; // Instead of type Data, could be any type.
> };
>
> class Data // Mock data type
> {
> methods
> .
> .
> Bounding_Box bounds; // Must have this!
> };
>
> Not sure if that got my point across :([/color]

You could just write the code and let the compiler complain about it when it
discovers that the template argument doesn't contain the required member.
You also might be able to cook up a "concept check" (similar to boost's
library, or perhaps using it) that checks for the member, but I wouldn't
know if that's possible without doing some research.

--
David Hilsee


Closed Thread