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 

Re: Template Pointer

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Matthew Collett
Guest





PostPosted: Sat Jun 28, 2003 11:15 pm    Post subject: Re: Template Pointer Reply with quote



In article <a2c25881.0306270828.72ff41e1 (AT) posting (DOT) google.com>,
[email]ashley.l.raiteri (AT) boeing (DOT) com[/email] (Ashley Raiteri) wrote:

Quote:
While the question may expose my ignorance, I have been struggling with
the
notion of having a pointer to a template class...

template <typename T
class generic
{
public:
void Do (T arg);
}

****
elsewhere

void mymethod (generic<>* tptr) {

tptr->Do ( myarg);
}

What this kind of thing shows is a desite to make use of templates
in a way that simply cannot be resolve at compile time.

Or can it?

Yes, it can. But the function that uses the pointer to the template
class must itself be a template, with the same arguments:

template <typename T>
void mymethod (generic<T>* tptr) {

tptr->Do ( myarg);
}


Best wishes,
Matthew Collett

--
Those who assert that the mathematical sciences have nothing to say
about the good or the beautiful are mistaken. -- Aristotle


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Mirek Fidler
Guest





PostPosted: Sun Jun 29, 2003 2:04 pm    Post subject: Re: Template Pointer Reply with quote



Quote:
void mymethod (generic<>* tptr) {

tptr->Do ( myarg);
}

You can use

template <class T>
void mymethod(generic<T> *tptr) { tptr->Do(); }

Quote:
What this kind of thing shows is a desite to make use of templates
in a way that simply cannot be resolve at compile time.

Or can it?

Is there any notion of a pointer to a template, or any mechanism that
can be used (first choice is probably a nontemplate base class, but
since
you can't implement a virtual template function, there'd be no way to
access
the derived template classes methods [ at least not the templatized
ones
]
from the base class pointer)?

Perhaps you cannot implement virtual template function but you do
not need either. What you need is vitrual member function of class
template:

struct Base {
virtual void Do();
}

template <class T>
class Generic : public Base {
virtual void Do() { .... }
}

void mymethod(Base *q) { q->Do(); }

Mirek



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) 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.