[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Interfaces in C++

Axel Gallus

10/6/2008 6:44:00 PM

If I declare a function pure virtual:

class A
{
virtual void myfunc() = 0;
}

and I derive a class from A:

class B : public A
{
void anotherfunc();
}

when I compile this program,
the compiler doesn't complain that there is no method myfunc() implemented
in B.
Is there a way to enforce such a compiler error/warning?

Regards

R4DIUM

30 Answers

Victor Bazarov

10/6/2008 6:50:00 PM

0

A.Gallus wrote:
> If I declare a function pure virtual:
>
> class A
> {
> virtual void myfunc() = 0;
> }
;

>
> and I derive a class from A:
>
> class B : public A
> {
> void anotherfunc();
> }

;
>
> when I compile this program,
> the compiler doesn't complain that there is no method myfunc()
> implemented in B.
> Is there a way to enforce such a compiler error/warning?

Yes. Try to instantiate your 'B' class or at least declare a function
that returns 'B' by value.

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

Axel Gallus

10/6/2008 7:13:00 PM

0

Bingo, that did the job, thx!




"Victor Bazarov" <v.Abazarov@comAcast.net> schrieb im Newsbeitrag
news:gcdmkq$ld7$1@news.datemas.de...
> A.Gallus wrote:
>> If I declare a function pure virtual:
>>
>> class A
>> {
>> virtual void myfunc() = 0;
>> }
> ;
>
>>
>> and I derive a class from A:
>>
>> class B : public A
>> {
>> void anotherfunc();
>> }
>
> ;
>>
>> when I compile this program,
>> the compiler doesn't complain that there is no method myfunc()
>> implemented in B.
>> Is there a way to enforce such a compiler error/warning?
>
> Yes. Try to instantiate your 'B' class or at least declare a function
> that returns 'B' by value.
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask

Maik

10/6/2008 7:46:00 PM

0

On 6 Okt., 21:12, "A.Gallus" <u...@rz.uni-karlsruhe.de> wrote:
> Bingo, that did the job, thx!

>> class A
>> {
>>    virtual void myfunc() = 0;
>> };

>> class B : public A
>> {

You should consider to prefer
class B : public virtual A
to avoid being bitten by:
http://www.parashift.com/c++-faq-lite/multiple-inheritance.htm...
which will most likely happen if one uses the interface pattern.

Best,
-- Maik

anon

10/7/2008 6:08:00 AM

0

Maik wrote:
> On 6 Okt., 21:12, "A.Gallus" <u...@rz.uni-karlsruhe.de> wrote:
>> Bingo, that did the job, thx!
>
>>> class A
>>> {
>>> virtual void myfunc() = 0;
>>> };
>
>>> class B : public A
>>> {
>
> You should consider to prefer
> class B : public virtual A
> to avoid being bitten by:
> http://www.parashift.com/c++-faq-lite/multiple-inheritance.htm...
> which will most likely happen if one uses the interface pattern.

I think that is worse performance wise then just this:
class B : public A
At least thats what I read here:
http://www.agner.org/optimize...

Chris Becke

10/7/2008 8:47:00 AM

0

You cant do that. On many platforms interfaces are a binary interop used and supported by languages other than c++.

In order to interop however, the vtable for the interface MUST be contiguous.

Clearly if you are working in a ahomogonous c++ only environment you can use virtual like that. But, despite the c++ standards body being unwilling to acknoledge it, c++ vtables *are* used for binary interop rendering this keyword worse than useless.

"Maik" <Beckmann.Maik@googlemail.com> wrote in message news:f4375d51-1d59-4371-95bd-e7a437664ce3@c60g2000hsf.googlegroups.com...
On 6 Okt., 21:12, "A.Gallus" <u...@rz.uni-karlsruhe.de> wrote:
> Bingo, that did the job, thx!

>> class A
>> {
>> virtual void myfunc() = 0;
>> };

>> class B : public A
>> {

You should consider to prefer
class B : public virtual A
to avoid being bitten by:
http://www.parashift.com/c++-faq-lite/multiple-inheritance.htm...
which will most likely happen if one uses the interface pattern.

Best,
-- Maik

Triple-DES

10/7/2008 10:33:00 AM

0

On 7 Okt, 10:46, "Chris Becke" <chris.be...@gmail.com> wrote:
[Top-posted in reply to an example of virtual inheritance]
> You cant do that. On many platforms interfaces are a binary interop used and supported by languages other than c++.
>
> In order to interop however, the vtable for the interface MUST be contiguous.
>
> Clearly if you are working in a ahomogonous c++ only environment you can use virtual like that.  But, despite the c++ standards body being unwilling to acknoledge it, c++ vtables *are* used for binary interop rendering this keyword worse than useless.

Platform / vendor-specific issues are mostly irrelevant here. Virtual
inheritance is part of the C++ language. Perhaps you intended to
write: "I advise you not to do that"?

James Kanze

10/7/2008 11:36:00 AM

0

On Oct 7, 10:46 am, "Chris Becke" <chris.be...@gmail.com> wrote:
> You cant do that. On many platforms interfaces are a binary
> interop used and supported by languages other than c++.

In which case, you must declare them ``extern "whatever"'', and
obey the contraints of that language. Or use some external
language (e.g. Corba) to define them. That's a completely
different problem, and largely irrelevant for most uses.

> In order to interop however, the vtable for the interface MUST
> be contiguous.

That depends entirely on the other language(s). In most cases,
you can't use virtual functions at all, without some sort of
intermediate code.

> Clearly if you are working in a ahomogonous c++ only
> environment you can use virtual like that. But, despite the
> c++ standards body being unwilling to acknoledge it, c++
> vtables *are* used for binary interop rendering this keyword
> worse than useless.

That is, of course, complete bullshit. *IF* you're writing to
some other language, then obviously, you have to respect the
contraints of that language, and work with a common subset.
(The most frequent case of this is, of course, C, since almost
all operating systems today define their API in C.) But except
for the OS API, that's really a rare case, and certainly not
something that most developers (or the standards committee)
need to take into consideration. (Also, FWIW: virtual
inheritance was around long before the standard.)

--
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

James Kanze

10/7/2008 11:37:00 AM

0

On Oct 7, 8:07 am, anon <a...@no.invalid> wrote:
> Maik wrote:
> > On 6 Okt., 21:12, "A.Gallus" <u...@rz.uni-karlsruhe.de> wrote:
> >> Bingo, that did the job, thx!

> >>> class A
> >>> {
> >>> virtual void myfunc() = 0;
> >>> };

> >>> class B : public A
> >>> {

> > You should consider to prefer
> > class B : public virtual A
> > to avoid being bitten by:
> > http://www.parashift.com/c++-faq-lite/multiple-inheritance.html...
> > which will most likely happen if one uses the interface pattern.

> I think that is worse performance wise then just this:
> class B : public A
> At least thats what I read
> here:http://www.agner.org/optimize...

Another site to avoid. If you have a performance problem,
measure and see. Until then, using virtual inheritance when
inheriting from an interface is probably a good idea; in
general, virtual inheritance should probably be the default.

--
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

Pete Becker

10/7/2008 12:32:00 PM

0

On 2008-10-07 02:07:33 -0400, anon <anon@no.invalid> said:

> Maik wrote:
>> On 6 Okt., 21:12, "A.Gallus" <u...@rz.uni-karlsruhe.de> wrote:
>>> Bingo, that did the job, thx!
>>
>>>> class A
>>>> {
>>>> virtual void myfunc() = 0;
>>>> };
>>
>>>> class B : public A
>>>> {
>>
>> You should consider to prefer
>> class B : public virtual A
>> to avoid being bitten by:
>> http://www.parashift.com/c++-faq-lite/multiple-inheritance.htm...
>> which will most likely happen if one uses the interface pattern.
>
> I think that is worse performance wise then just this:
> class B : public A
> At least thats what I read here:
> http://www.agner.org/optimize...

And int is faster than double, so I won't use floating-point.
Unfortunately, that often gives the wrong answer.

Seriously: virtual and non-virtual inheritance have rather different
meanings, and performance concerns (whether real or imaginary) never
overrule correct semantics.

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

Maik

10/7/2008 8:58:00 PM

0

On 7 Okt., 08:07, anon <a...@no.invalid> wrote:
> Maik wrote:
> > You should consider to prefer
> >   class B : public virtual A
> > to avoid being bitten by:
> >  http://www.parashift.com/c++-faq-lite/multiple-inheritance.htm...
> > which will most likely happen if one uses the interface pattern.
>
> I think that is worse performance wise then just this:
> class B : public A
> At least thats what I read here:http://www.agner.org/optimize...

Hi! Virtual inheritance has worse performance, thats right. But as
long as an interface lookup at runtime triggers an much bigger chuck
of operations (say matrix operations or stl action) the over all
performance drop is small.

For performance critical parts I would even consider to avoid virtual
member functions. But for interfaces virtual inheritance is most
likely the way to go.

Best,
-- Maik