473,385 Members | 1,279 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,385 software developers and data experts.

Templates problem

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
Jul 22 '05 #1
2 1084
Christopher wrote:
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
Jul 22 '05 #2
"Christopher" <cp***@austin.rr.com> wrote in message
news:71**************************@posting.google.c om...
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.

--
David Hilsee
Jul 22 '05 #3

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

Similar topics

4
by: franky.backeljauw | last post by:
Hello, I have a problem with using a copy constructor to convert an object of a templated class to object of another templated class. Let me first include the code (my question is below): ...
5
by: Tom Alsberg | last post by:
Hi there... I'm recently trying to get a bit acquainted with XML Schemas and XSL. Now, I have a few questions about XSL stylesheets and templates: * Is there a way to "enter" a child element...
1
by: Bo Xu | last post by:
Object of Combination By Bo Xu Introduction A combination of n things, taken s at a time, often referred as an s-combination out of n, is a way to select a subset of size s from a given set of...
13
by: Winbatch | last post by:
Hi, If this should be directed to another group, please let me know... I've been working with templates for a few weeks and have been able to develop some nice code on solaris using the Forte C++...
7
by: Jon Slaughter | last post by:
#pragma once #include <vector> class empty_class { }; template <int _I, int _J, class _element, class _property> class RDES_T {
25
by: Ted | last post by:
I'm putting the posts that follow here (hopefully they will follow here!) because they were rejected in comp.lang.c++.moderated. It behooves anyone reading them to first read the the thread of the...
5
by: Eric Fortier | last post by:
Hi all, Last year I posted a message regarding templates use in blitter functions, but the single answer I got didn't help. My problem is that I was writing blitter functions which takes a...
1
by: jimmyB | last post by:
Hi, I have one query and I hope you people can solve it. Problem is related to Smarty Caching Database Templates.. Most popular web applications and forums like IPB (invision power board) and...
5
by: ryanoasis | last post by:
Working on a C++ assignment and I cant figure out the problems I am having w/ Templates and Subclasses. I know there are issues with templates and certain compilers so I am not sure what the...
104
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.