pure virtual function in template class

  • Thread starter Mike -- Email Ignored
  • Start date
M

Mike -- Email Ignored

Is a pure virtual function in allowed in a template
base class? In any case, I have one working. Am I
skating on thin ice?
Thanks,
Mike.
 
P

puzzlecracker

Is a pure virtual function in allowed in a template
base class? In any case, I have one working. Am I
skating on thin ice?
Thanks,
Mike.


Yes, it can be in the template class, but it can NOT be a virtual
template member function
 
M

Mike -- Email Ignored

Yes, it can be in the template class, but it can NOT be a virtual
template member function

I don't understand how it could be pure, but not a member.
In any case, here is code that works on my system showing
exactly what I mean.
Mike.

// virt_temp.cc 07/10/08
#include <iostream>
using namespace std;

template <class TYP>
class BaseT
{
protected:
BaseT(TYP x,TYP y) : x_(x),y_(y){}
void doAll(){cout << "x_="<<x_<<',';doChild(y_);cout<<endl;}
virtual void doChild(TYP a)=0;// pure virtual member function
private:
TYP x_;
TYP y_;
};

class Child : protected BaseT<int>
{
public:
Child() : BaseT<int>(1,2){}
void doThings(){doAll();}
private:
virtual void doChild(int a);
};

void Child::doChild(int a){cout<<"a="<<a;}

int main(int argc, const char* argv[])
{
Child child;
child.doThings();
}
 
M

Mike -- Email Ignored

On Thu, 10 Jul 2008 10:56:16 -0400, Victor Bazarov wrote:

[...]
It's the case when too much information actually hurt. What clacker is
telling you is that you can't have a template member declared virtual
(pure or not):

class foo {
template<class T> virtual void bar(T const&); // error
};

, that's, all.

V

Then my working example is just dumb luck?
Or might it be a non-standard gnu add-on?
Mike.
 
M

Mike -- Email Ignored

Mike said:
On Thu, 10 Jul 2008 10:56:16 -0400, Victor Bazarov wrote:

[...]
It's the case when too much information actually hurt. What clacker
is telling you is that you can't have a template member declared
virtual (pure or not):

class foo {
template<class T> virtual void bar(T const&); // error
};

, that's, all.

V

Then my working example is just dumb luck?

"Dumb luck"? I am not sure how that is applicable here. Your example
has no virtual functions that are member templates.

Then what is my function:

virtual void doChild(TYP a)=0;// pure virtual member function

Why is this ok?

[...]

Mike.
 
M

Mike -- Email Ignored

Mike -- Email Ignored wrote: [...]
Then what is my function:

virtual void doChild(TYP a)=0;// pure virtual member function

It's a pure virtual function, a member of the class template. Since it
is a member of a template, it is a template itself, but it's not a
member template. It's a template member. Confusing, isn't it?
[...]

Yes, thanks, it is clear now (pardon my laughter). Is there somewhere
is the standard I might see this?

Mike.
 
J

James Kanze

Is a pure virtual function in allowed in a template base
class? In any case, I have one working. Am I skating on thin
ice?

No. Why should there be any problem? The instantiation of a
class template is just like any other class. The same rules
apply.
 
M

Mike -- Email Ignored

No. Why should there be any problem? The instantiation of a class
template is just like any other class. The same rules apply.

How about, then, a template class virtual member function that
is not pure? It is my understanding that such a virtual function
may not be inline, but, at least on the gnu c++ compiler, non-pure
functions of a template class must be inline (unless there have
been recent developments I do not know about).

Mike.
 
J

James Kanze

How about, then, a template class virtual member function that
is not pure?

Again, the same rules apply as with any other class. No
problem.
It is my understanding that such a virtual function
may not be inline,

Why not? One idiom (which used to be common, before templates)
even requires them to be inline.
but, at least on the gnu c++ compiler, non-pure functions of a
template class must be inline (unless there have been recent
developments I do not know about).

Nonsense. G++ has never had this restriction.
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top