Templates problem

C

Christopher

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
 
V

Victor Bazarov

Christopher said:
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 :(

// 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
 
D

David Hilsee

Christopher said:
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 :(

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.
 

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,175
Messages
2,570,947
Members
47,498
Latest member
yelene6679

Latest Threads

Top