Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   pure virtual function in template class (http://www.velocityreviews.com/forums/t624863-pure-virtual-function-in-template-class.html)

Mike -- Email Ignored 07-10-2008 12:49 PM

pure virtual function in template class
 
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.

puzzlecracker 07-10-2008 12:53 PM

Re: pure virtual function in template class
 
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

Mike -- Email Ignored 07-10-2008 02:52 PM

Re: pure virtual function in template class
 
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();
}

Mike -- Email Ignored 07-10-2008 03:25 PM

Re: pure virtual function in template class
 
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.


Mike -- Email Ignored 07-10-2008 05:20 PM

Re: pure virtual function in template class
 
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 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.


Mike -- Email Ignored 07-10-2008 05:42 PM

Re: pure virtual function in template class
 
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.


James Kanze 07-11-2008 09:08 AM

Re: pure virtual function in template class
 
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:james.kanze@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

Mike -- Email Ignored 07-13-2008 01:36 AM

Re: pure virtual function in template class
 
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.

James Kanze 07-13-2008 08:36 AM

Re: pure virtual function in template class
 
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:james.kanze@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


All times are GMT. The time now is 04:49 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57