problem with overloaded pure virtual functions

2 views
Skip to first unread message

A. Saksena

unread,
Jan 14, 2005, 5:01:57 AM1/14/05
to
i all,
I am trying to compile following code :-
=======================================
#include <iostream>
#include <string>
namespace tlm
{
template <typename REQUEST, typename RESPONSE >
class tlm_transport_if
{
public:
// Virtual function: for implementation of transport layer
virtual RESPONSE transport (const REQUEST &) = 0;
}; // end of class tlm_transport_if

}

class master_out_if
{
private:
class inherit: public tlm::tlm_transport_if<int,bool >,
public tlm::tlm_transport_if<char,bool >,
public tlm::tlm_transport_if<double,bool >
{};
public:
typedef inherit type;
};


class x:public master_out_if::type
{
public:
bool transport (const int&){return true;}
bool transport (const char &){return true;}
bool transport (const double &){return true;}

};

int main()//this can be bus model

{
master_out_if::type *var1=new x;
bool result=var1->transport((int)(100));

}
==========================================


The problem is that I get a compile time error which says

-----------------------------------------------------------
test2.cpp: In function `int main()':
test2.cpp:55: cannot allocate an object of type `x'
test2.cpp:55: because the following virtual functions are abstract:
test2.cpp:13: RESPONSE tlm::tlm_transport_if<REQUEST,
RESPONSE>::transport(const REQUEST&) [with REQUEST = double, RESPONSE =
bool]
test2.cpp:56: request for member `transport' is ambiguous
test2.cpp:13: candidates are: RESPONSE tlm::tlm_transport_if<REQUEST,
RESPONSE>::transport(const REQUEST&) [with REQUEST = double, RESPONSE =
bool]
test2.cpp:13: RESPONSE tlm::tlm_transport_if<REQUEST,
RESPONSE>::transport(const REQUEST&) [with REQUEST = char, RESPONSE =
bool]
test2.cpp:13: RESPONSE tlm::tlm_transport_if<REQUEST,
RESPONSE>::transport(const REQUEST&) [with REQUEST = int, RESPONSE =
bool]
--------------------------------------------------------------------


Does that mean overloading of virtual functions is not allowed?. If not how
I can implement this functionality


Abhishek

Mike Wahler

unread,
Jan 14, 2005, 3:00:11 PM1/14/05
to

" A. Saksena" <abhishek...@philips.com> wrote in message
news:cs85av$o5j$1...@voyager.news.surf.net...

No, it means that an object of a type with a *pure* virtual
function cannot be instantiated, you need to provide an
implementation of the virtual function.

-Mike


B. v Ingen Schenau

unread,
Jan 14, 2005, 4:01:41 PM1/14/05
to
A. Saksena wrote:

You can overload virtual functions, but what you can't do is inherit a
function (or object) with the same *name* from multiple base classes.
The compiler finds that the name 'transport' can come from one of three
different base classes and for that reason it declares the call as
being ambiguous. This is done before overload resolution even starts.

To get this functionality working, you must make it explicit to the
compiler that you want to let the overload resolution loose on the
'transport' functions from *all* the base classes. This can be achieved
by explicitly pulling the name 'transport' from each of the base
classes into the master_out_if::inherit class, like this:

class master_out_if
{
private:
class inherit: public tlm::tlm_transport_if<int,bool >,
public tlm::tlm_transport_if<char,bool >,
public tlm::tlm_transport_if<double,bool >
{
public:

// I can't choose, use transport from all three base classes :-)
using tlm::tlm_transport_if<int,bool >::transport;
using tlm::tlm_transport_if<char,bool >::transport;
using tlm::tlm_transport_if<double,bool >::transport;
};
public:
typedef inherit type;
};

>
>
> Abhishek

Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/

Chris ( Val )

unread,
Jan 15, 2005, 9:39:12 AM1/15/05
to

"B. v Ingen Schenau" <ba...@ingen.ddns.info> wrote in message
news:34qq9mF...@individual.net...

| A. Saksena wrote:
|
| > i all,
| > I am trying to compile following code :-

[snip code]

| > Does that mean overloading of virtual functions is not allowed?. If
| > not how I can implement this functionality
|
| You can overload virtual functions, but what you can't do is inherit a
| function (or object) with the same *name* from multiple base classes.
| The compiler finds that the name 'transport' can come from one of three
| different base classes and for that reason it declares the call as
| being ambiguous. This is done before overload resolution even starts.
|
| To get this functionality working, you must make it explicit to the
| compiler that you want to let the overload resolution loose on the
| 'transport' functions from *all* the base classes. This can be achieved
| by explicitly pulling the name 'transport' from each of the base
| classes into the master_out_if::inherit class, like this:
|
| class master_out_if
| {
| private:
| class inherit: public tlm::tlm_transport_if<int,bool >,
| public tlm::tlm_transport_if<char,bool >,
| public tlm::tlm_transport_if<double,bool >
| {
| public:
| // I can't choose, use transport from all three base classes :-)
| using tlm::tlm_transport_if<int,bool >::transport;
| using tlm::tlm_transport_if<char,bool >::transport;
| using tlm::tlm_transport_if<double,bool >::transport;
| };
| public:
| typedef inherit type;
| };

Hi Bart.

I'm curious if you tested what you have suggested ?

The code looks quite weird and convoluted to me :-),
so I thought I'd test it out, but I got the following
errors (Well, in BCB5 and MinGW at least):

I attempted to try 'Comeau' on-line, but I cannot get
to the website at all.

Borland Builder 5.0 UP1
-----------------------
[C++ Error] Default.cpp(28):
E2507 'tlm::tlm_transport_if<int,bool>'
is not a direct base class of 'master_out_if'

[C++ Error] Default.cpp(29):
E2507 'tlm::tlm_transport_if<char,bool>'
is not a direct base class of 'master_out_if'

[C++ Error] Default.cpp(30):
E2507 'tlm::tlm_transport_if<double,bool>'
is not a direct base class of 'master_out_if'

MinGW
-----
Template_Main.cpp:19: error: type `master_out_if'
is not a base type for type `tlm::tlm_transport_if<int, bool>'

Template_Main.cpp:19: error: type `master_out_if'
is not a base type for type ` tlm::tlm_transport_if<char, bool>'

Template_Main.cpp:19: error: type `master_out_if'
is not a base type for type ` tlm::tlm_transport_if<double, bool>'

Cheers.
Chris Val


Greg Comeau

unread,
Jan 15, 2005, 11:32:27 AM1/15/05
to
In article <34so8gF...@individual.net>,

Chris \( Val \) <chri...@bigpond.com.au> wrote:
>I attempted to try 'Comeau' on-line, but I cannot get
>to the website at all.

In a very rare situation, our ISP has had the rug pulled
out from under them from what appears to be some sort
of attack this morning that apparently needs to involve
"the registrars" to fix. I'm guessing it will be back up
today. I'll repost again when it is known it wil stay up,
though of course you can all try before that in case
it comes up temporarily in whole or in part.
Thanks all for your patience.
--
Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

B. v Ingen Schenau

unread,
Jan 16, 2005, 5:29:57 AM1/16/05
to
Chris ( Val ) wrote:

>
> "B. v Ingen Schenau" <ba...@ingen.ddns.info> wrote in message
> news:34qq9mF...@individual.net...

> | class master_out_if
> | {
> | private:
> | class inherit: public tlm::tlm_transport_if<int,bool >,
> | public tlm::tlm_transport_if<char,bool >,
> | public tlm::tlm_transport_if<double,bool >
> | {
> | public:
> | // I can't choose, use transport from all three base classes :-)
> | using tlm::tlm_transport_if<int,bool >::transport;
> | using tlm::tlm_transport_if<char,bool >::transport;
> | using tlm::tlm_transport_if<double,bool >::transport;
> | };
> | public:
> | typedef inherit type;
> | };
>
> Hi Bart.
>
> I'm curious if you tested what you have suggested ?

Yes, I did test the code before posting. I actually pasted this code
from my editor.

>
> The code looks quite weird and convoluted to me :-),
> so I thought I'd test it out, but I got the following
> errors (Well, in BCB5 and MinGW at least):
>
> I attempted to try 'Comeau' on-line, but I cannot get
> to the website at all.
>
> Borland Builder 5.0 UP1
> -----------------------
> [C++ Error] Default.cpp(28):
> E2507 'tlm::tlm_transport_if<int,bool>'
> is not a direct base class of 'master_out_if'

This message suggests to me that you placed the using declaration in the
class master_out_if, but in the code that I posted, the using
declaration is in the (nested!) class master_out_if::inherit, which
*does* inherit from tlm::tlm_transport_if<int, bool>.

<snip - more similar errors, also from MinGW >

> Cheers.
> Chris Val

Chris ( Val )

unread,
Jan 16, 2005, 6:44:07 AM1/16/05
to

"B. v Ingen Schenau" <ba...@ingen.ddns.info> wrote in message
news:34uu15F...@individual.net...

| Chris ( Val ) wrote:
|
| >
| > "B. v Ingen Schenau" <ba...@ingen.ddns.info> wrote in message
| > news:34qq9mF...@individual.net...
| > | class master_out_if
| > | {
| > | private:
| > | class inherit: public tlm::tlm_transport_if<int,bool >,
| > | public tlm::tlm_transport_if<char,bool >,
| > | public tlm::tlm_transport_if<double,bool >
| > | {
| > | public:
| > | // I can't choose, use transport from all three base classes :-)
| > | using tlm::tlm_transport_if<int,bool >::transport;
| > | using tlm::tlm_transport_if<char,bool >::transport;
| > | using tlm::tlm_transport_if<double,bool >::transport;
| > | };
| > | public:
| > | typedef inherit type;
| > | };
| >
| > Hi Bart.
| >
| > I'm curious if you tested what you have suggested ?
|
| Yes, I did test the code before posting. I actually pasted this code
| from my editor.

[snip]

| > [C++ Error] Default.cpp(28):
| > E2507 'tlm::tlm_transport_if<int,bool>'
| > is not a direct base class of 'master_out_if'
|
| This message suggests to me that you placed the using declaration in the
| class master_out_if, but in the code that I posted, the using
| declaration is in the (nested!) class master_out_if::inherit, which
| *does* inherit from tlm::tlm_transport_if<int, bool>.

[snip]

Awe - Good catch Bart :-)

That'll teach me for playing with code instead of
being in bed - Thanks for the *wakeup call* <g>.

FWIW, under BCB5 the code now invokes a pure function
call, and that is not allowed, but it seems fine under
MinGW.

Cheers.
Chris Val


B. v Ingen Schenau

unread,
Jan 16, 2005, 10:57:50 AM1/16/05
to
Chris ( Val ) wrote:

[regarding code that inherits from a template instance which contains a
pure virtual function]

> FWIW, under BCB5 the code now invokes a pure function
> call, and that is not allowed, but it seems fine under
> MinGW.

Maybe BCB can't handle it that a pure virtual function that is part of a
template (instance) gets overridden by a function from a non-templated
class.
I am myself also not 100% sure that such a situation is fully
conforming.

James Dennett

unread,
Jan 16, 2005, 1:32:21 PM1/16/05
to
B. v Ingen Schenau wrote:
> Chris ( Val ) wrote:
>
> [regarding code that inherits from a template instance which contains a
> pure virtual function]
>
>
>>FWIW, under BCB5 the code now invokes a pure function
>>call, and that is not allowed, but it seems fine under
>>MinGW.
>
>
> Maybe BCB can't handle it that a pure virtual function that is part of a
> template (instance) gets overridden by a function from a non-templated
> class.

I can't comment on that, I don't have BCB handy to test on.
(Seems like too fundamental a thing to have got wrong, but
compilers have been known to have such bugs, particularly in
older versions of C++ compilers.)

> I am myself also not 100% sure that such a situation is fully
> conforming.

It's just fine, and indeed normal; the class that comes from
instantiating the template obeys all of the same rules as any
other class, including that its virtual members can be
overridden by members of derived class if they have the same
parameter types and a suitable (possibly covariant) return
type.

-- James

Chris ( Val )

unread,
Jan 17, 2005, 5:41:19 AM1/17/05
to

"James Dennett" <jden...@acm.org> wrote in message news:PqyGd.8154$ru.3649@fed1read07...

| B. v Ingen Schenau wrote:
| > Chris ( Val ) wrote:

Hi James.

| > [regarding code that inherits from a template instance which contains a
| > pure virtual function]
| >
| >
| >>FWIW, under BCB5 the code now invokes a pure function
| >>call, and that is not allowed, but it seems fine under
| >>MinGW.
| >
| >
| > Maybe BCB can't handle it that a pure virtual function that is part of a
| > template (instance) gets overridden by a function from a non-templated
| > class.
|
| I can't comment on that, I don't have BCB handy to test on.
| (Seems like too fundamental a thing to have got wrong, but
| compilers have been known to have such bugs, particularly in
| older versions of C++ compilers.)

This is true of some compilers, especially mine :-)

Actually, try re-throwing an exception object under BCB5,
with a static counter set up, and you will find the
destructor is called more than necessary.

Luckily I don't go throwing exceptions around all day,
especially if there is logic in the destructors to
re-allocate memory. An exceptional case and good argument
for the smart pointer :-)

| > I am myself also not 100% sure that such a situation is fully
| > conforming.
|
| It's just fine, and indeed normal; the class that comes from
| instantiating the template obeys all of the same rules as any
| other class, including that its virtual members can be
| overridden by members of derived class if they have the same
| parameter types and a suitable (possibly covariant) return
| type.

Ok, thanks.

FWIW, I find this code to be a very convoluted and confusing
hierarchy of objects. It looks as if though you've had to
travel 500 miles to get five feet to the left of you.

Cheers.
Chris Val


Greg Comeau

unread,
Jan 19, 2005, 10:07:31 AM1/19/05
to
In article <csbgir$f2b$1...@panix1.panix.com>,

Greg Comeau <com...@comeaucomputing.com> wrote:
>In article <34so8gF...@individual.net>,
>Chris \( Val \) <chri...@bigpond.com.au> wrote:
>>I attempted to try 'Comeau' on-line, but I cannot get
>>to the website at all.
>
>In a very rare situation, our ISP has had the rug pulled
>out from under them from what appears to be some sort
>of attack this morning that apparently needs to involve
>"the registrars" to fix. I'm guessing it will be back up
>today. I'll repost again when it is known it wil stay up,
>though of course you can all try before that in case
>it comes up temporarily in whole or in part.
>Thanks all for your patience.

Thanks again for your patience. You may have heard about
the panix.com domain name hijacking, which was unusual in
that the domain was an ISPs and furthermore was registrar locked.
It wasn't unusual in that the registar(s) responded poorly,
had no emergency plans or policies, and have admitted their fault.

Well, comeaucomputing.com was an artifact of that.
The good news is that some people did not get to really
notice the problem do the DNS caching, and there was some
staff work done to keep things alive before the registrars
put things back the way they were supposed to be.

If anybody ever finds http://www.comeaucomputing.com/tryitout
"down" in any way feel free to email us, realizing though that
domain hijacking may mean email bounces too :(

Anyway, thanks for the emails asking if we're ok.
We're alive and well and packed with surprises for 2005.

Anyway, let's not discuss this here.
For some gory details, starts from panix.com and
your favorite search engine.

Reply all
Reply to author
Forward
0 new messages