C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Template of interface?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
bonj
Guest





PostPosted: Sun Feb 27, 2005 12:19 am    Post subject: Template of interface? Reply with quote



Hello
How do I declare a template class such that the template class T must
implement a certain interface?

e.g.
interface IProvideID
{
int MyID;
};

template<type T>
class ListOfIDs
{
public int add(T& newitem)
{
return newitem.MyID; //(this is what I want to do)
}
};


Back to top
Shezan Baig
Guest





PostPosted: Sun Feb 27, 2005 1:35 am    Post subject: Re: Template of interface? Reply with quote




bonj wrote:
Quote:
Hello
How do I declare a template class such that the template class T must
implement a certain interface?

e.g.
interface IProvideID
{
int MyID;
};

template<type T
class ListOfIDs
{
public int add(T& newitem)
{
return newitem.MyID; //(this is what I want to do)
}
};



You already did it. If the 'T' type does not have a 'MyID' member,
then ListOfIDs
-shez-


Back to top
Ken Hagan
Guest





PostPosted: Tue Mar 01, 2005 9:22 am    Post subject: Re: Template of interface? Reply with quote



Shezan Baig wrote:
Quote:
You already did it. If the 'T' type does not have a 'MyID' member,
then ListOfIDs<T>::add(...) will fail to compile.

Not quite. I think bonj wants to ensure that the "MyID" that the
compiler finds is IProvideID::MyID and not the MyID of a completely
unrelated interface. But that isn't much harder.

template<type T> class ListOfIDs
{
public int add(T& newItem)
{
static_cast<IProvideID&>(newItem); // insist on the interface
return newItem.MyID;
}
};

This code will fail to compile if T isn't based (perhaps indirectly)
on IProvideID.

Back to top
Mike Smith
Guest





PostPosted: Wed Mar 02, 2005 4:10 pm    Post subject: Template or interface? - Related question Reply with quote

bonj wrote:

Quote:
Hello
How do I declare a template class such that the template class T must
implement a certain interface?

I have a related question. Which is considered better for writing
interfaces in C++: abstract base classes, or templates - and why? I.e.
when is one preferable over the other?

Let's suppose I want to specify an interface for classes that can
"pickle" themselves; i.e. read/write their state information to a "flat"
binary storage (for serialization, or message-passing, etc.) It occurs
to me that there are two ways one could do this:

typedef std::vector<unsigned char> ByteVector;

struct IPickle
{
virtual void Pickle(ByteVector &bv) = 0; // write state to vector
virtual void Unpickle(ByteVector &bv) = 0; // get state from vector
};

then one could create classes that inherit from IPickle and implement
the functions:

class PickleSample : public IPickle
{
...
};

Or, one could use the Curiously Recurring Template Pattern:

template <class T> struct TPickle
{
void Pickle(ByteVector &bv) {T::Pickle(bv);}
void Unpickle(ByteVector &bv) {T::Unpickle(bv);}
};

class PickleSample : public TPickle<PickleSample>
{
...
};

What are the pros and cons of each? The ABC approach lets you do things
like this:

void PickleAndSendMessage(IPickle *p_obj)
{
ByteVector bv;
p_obj->Pickle(bv);
SendMessageUsingSomeTransport(&bv[0], bv.size());
}

i.e. where PickleAndSendMessage() can work with any object that
"exposes" the IPickle interface. If I understand correctly, this would
not be possible with the CRTP approach. I thought maybe that an
advantage of the CRTP would be that it would be easier to "bolt on"
Pickle functionality to an existing class, e.g.:

// pre-existing class
class Blorf
{
int foo, bar;
public:
// some functions here
};

// new class with pickling
class PickleBlorf : public Blorf, public TPickle<PickleBlorf>
{
public:
PickleBlorf() : Blorf() {}
void Pickle(ByteVector &bv) {/*write foo and bar into bv*/}
void Unpickle(ByteVector &bv) {/*get foo and bar from bv*/}
};

but then I realized that this could just as easily be done using the ABC
approach - instead of inheriting from TPickle<PickleBlorf> above, you
would just inherit from IPickle instead.

So what's the downside of the ABC approach? What am I missing about the
Curiously Recurring Template Pattern that's got everybody talking about
it? Why not just use ABCs instead? Is it the fact that they lead to
the creation of a vtable? Is that such a big deal?

Thanks,
--
Mike Smith

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.