By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,633 Members | 1,313 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,633 IT Pros & Developers. It's quick & easy.

pure virtual function in template class

P: n/a
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.
Jul 10 '08 #1
Share this Question
Share on Google+
13 Replies


P: n/a
On Jul 10, 8:49 am, Mike -- Email Ignored <m_d_berger_1...@yahoo.com>
wrote:
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
Jul 10 '08 #2

P: n/a
On Thu, 10 Jul 2008 05:53:45 -0700, puzzlecracker wrote:
On Jul 10, 8:49 am, Mike -- Email Ignored <m_d_berger_1...@yahoo.com>
wrote:
>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
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();
}
Jul 10 '08 #3

P: n/a
Mike -- Email Ignored wrote:
On Thu, 10 Jul 2008 05:53:45 -0700, puzzlecracker wrote:
>On Jul 10, 8:49 am, Mike -- Email Ignored <m_d_berger_1...@yahoo.com>
wrote:
>>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

I don't understand how it could be pure, but not a member.
[..]
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 Tvirtual void bar(T const&); // error
};

, that's, all.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 10 '08 #4

P: n/a
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 Tvirtual 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.

Jul 10 '08 #5

P: n/a
Mike -- Email Ignored wrote:
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 Tvirtual 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.
Or might it be a non-standard gnu add-on?
No, it seems to be fine (except that you forgot to include <ostream>,
but it's a common mistake often offset by the fact that the compiler
includes one along with <iostream>, behind the scenes).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 10 '08 #6

P: n/a
On Thu, 10 Jul 2008 12:48:36 -0400, Victor Bazarov wrote:
Mike -- Email Ignored wrote:
>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 Tvirtual 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.

Jul 10 '08 #7

P: n/a
On 2008-07-10 13:20:37 -0400, Mike -- Email Ignored
<m_*************@yahoo.comsaid:
>
Then what is my function:

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

Why is this ok?
Because a member function of a template class is not the same thing as
a template member function.

template <class T>
class C
{
public:
virtual void f(); // member function of template class
template <class Uvoid g(); // template member function of template class
};

class D
{
public:
template <class Uvoid g(); // template member function of class
};

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jul 10 '08 #8

P: n/a
Mike -- Email Ignored wrote:
On Thu, 10 Jul 2008 12:48:36 -0400, Victor Bazarov wrote:
>Mike -- Email Ignored wrote:
>>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 Tvirtual 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
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?
>
Why is this ok?
Why wouldn't it be? Every instance of your class template gets its own
virtual function. For example, your BaseT<inthas

virtual void BaseT<int>::doChild(int) = 0;

and it's pure, so any derived class has to override it if it wants to be
non-abstract (and your 'Child' provides it); and if you had some other
instance of your template, say, 'BaseT<std::vector<std::string', then
it would have

virtual void BaseT<std::vector<std::string
::doChild(std::vector<std::string) = 0;

which also has to be overridden in the derived class, should you want to
instantiate the derived class by itself (make it non-abstract).

Please feel free to ask more questions.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 10 '08 #9

P: n/a
On Thu, 10 Jul 2008 13:28:40 -0400, Victor Bazarov wrote:
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.

Jul 10 '08 #10

P: n/a
Mike -- Email Ignored wrote:
On Thu, 10 Jul 2008 13:28:40 -0400, Victor Bazarov wrote:
>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?
See what, exactly? The whole chapter 14 is dedicated to templates, the
chapter 10 is dedicated to derived classes...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 10 '08 #11

P: n/a
On Jul 10, 2:49 pm, Mike -- Email Ignored <m_d_berger_1...@yahoo.com>
wrote:
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 11 '08 #12

P: n/a
On Fri, 11 Jul 2008 02:08:45 -0700, James Kanze wrote:
On Jul 10, 2:49 pm, Mike -- Email Ignored <m_d_berger_1...@yahoo.com>
wrote:
>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.
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.
Jul 13 '08 #13

P: n/a
On Jul 13, 3:36 am, Mike -- Email Ignored <m_d_berger_1...@yahoo.com>
wrote:
On Fri, 11 Jul 2008 02:08:45 -0700, James Kanze wrote:
On Jul 10, 2:49 pm, Mike -- Email Ignored <m_d_berger_1...@yahoo.com>
wrote:
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.
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 13 '08 #14

This discussion thread is closed

Replies have been disabled for this discussion.