473,378 Members | 1,544 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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.
Jul 10 '08 #1
13 7230
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: cppaddict | last post by:
Hi, I know that it is illegal in C++ to have a static pure virtual method, but it seems something like this would be useful when the following 2 conditions hold: 1. You know that every one...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
6
by: Dilip | last post by:
Hi All I have a pure virtual function in my base class that my derived instances override. What are the likely causes for VC++ 7.1 to complain of unresolved external symbol on that pure virtual...
3
by: Dmitry Prokoptsev | last post by:
Hello, I need to write a class for exceptions which can be thrown, caught, stored and thrown once again. I have written the following code: --- begin --- #include <string> class Exception...
3
by: nw | last post by:
Hi, I have three classes, a template pure virtual base class, a template derived class and a third which I would like to use to store copies of the derived class. The code looks like this: ...
6
by: Miguel Guedes | last post by:
Hello, I recently read an interview with Bjarne Stroustrup in which he says that pure abstract classes should *not* contain any data. However, I have found that at times situations are when it...
11
by: mathieu | last post by:
Hi there, I don't think I'll be able to describe my issue correctly, so instead I'll just give a pseudo C++ code I am struggling with. Basically I am looking for a 'pure virtual template'...
2
by: Markus Dehmann | last post by:
I have an abstract base class called Data. It has a pure virtual function virtual void write(std::ostream& out) =0; which writes the internal data to a stream. Now the problem is that this is...
7
by: Tonni Tielens | last post by:
I'm trying to create a pure virtual class describing an interface. Normally, when I do this I make the destructor pure virtual so that, even if there are no members in the class, it cannot be...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.