Re: member template -- reusing type information?
* rlwebbnafex@gmail.com:
Hello,
I have my inline class definition below. I have my member template
constructors which initialize my "data" object. The problem is the
CheckOne() function, which uses the T1 template argument. I want it to
iterate over "data" with the same T1 datatype that was used to
construct the data object in the IndividualRecord constructor. That is
it needs to remember what is T1 and use the same datatype. Every way I
tried gives an error, can you help? thanks
template <class DATA>
template<DATA>
class IndividualRecord
{
public:
DATA data;
public:
bool isWritten;
IndividualRecord(const IndividualRecord&
r):isWritten(false),data(r.data){}
IndividualRecord& operator==(const IndividualRecord& r)const
{
return data==r.data;
}
template <class T1>
IndividualRecord(const T1 p1):isWritten(false),data(p1){}
template <class T1, class T2>
IndividualRecord(const T1 p1,const T2 p2):isWritten(false),data(p1,p2)
{}
template <class T1, class T2, class T3>
IndividualRecord(const T1 p1,const T2 p2,const T3
p3):isWritten(false),data(p1,p2,p3){}
...
bool CheckOne()
{
bool results = 0;
vector<T1>::iterator Iter;
vector<T1>::iterator Iter2;
for (Iter = data.p1.begin(); Iter != data.p1.end(); Iter++ ) //
iterate over the inside variable
{
for (Iter2 = outside.data.p1.begin(); Iter !=
outside.data.p1.end(); Iter++) // iterate over outside variable
to see if anything matches
{
if (*Iter == *Iter2)
results = 1;
}
}
return results;
};
Your idea about treating your data member as being of a different type than it
is, is not workable.
It seems that what you're really after is having that member of some type
determined at run time.
The way to have different static and dynamic types is to use a pointer
(preferably a smart pointer).
If, on the other hand, you really mean to have that same static and dynamic
types for the data member, then something else must yield in order to be able to
vary the type.
And that something else would then be the ability to supply an object of
different and somewhat incompatible (wrt. iterators) type as initializer.
Cheers, & hth.,
- Alf
PS: I'm reasonably confident that you will not realize that the above is
answering your question. At least not at first. So, after having dismissed this
posting as mumbo-jumbo, think about it.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?