Topic: Legal C++ code?
Author: "Wil Evers" <wil@ittpub.nl>
Date: 1996/02/22 Raw View
In article <4gbilr$38m@dub-news-svc-1.compuserve.com> 71247.3221@compuserve.com (Don Wallace) writes:
[example of derived class calling pure virtual function in base class]
> I suspect that this is perfectly legal C++ code. Every statement taken
> individually is correct. But even if it linked, the net effect of
> calling bar.foo() would be to attempt to activate a pure virtual
> function (In Borland C++ this generates a RTL program halt - it says
> 'pure virtual function called'.)
>
> The statement/function
>
> > int foo() { return Foo::foo(); }
>
> refers to Foo::foo() which is inherently not defined. Hence the link
> error.
This is a common misconception: a pure virtual function may or may
not have a definition. It it doesn't have a definition and you try to
call it anyway, you will probably get a link-time- or run-time error or
your program could just go haywire; if it does have a definition, then
calling it is perfectly OK. The canonical example of a pure virtual
function that does have a definition is a pure virtual destructor:
without a body, such a class would not be very useful.
- Wil
Wil Evers, ITT Publitec Research and Development, <wil@ittpub.nl>
---
[ To submit articles: Try just posting with your newsreader. If that fails,
use mailto:std-c++@ncar.ucar.edu
FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: tim@franck.Princeton.EDU (Tim Hollebeek)
Date: 1996/02/16 Raw View
Is the following legal C++ code? g++ compiles it, but it doesn't link
(undefined function Foo::foo()). I'm suprised it compiles at all:
class Foo {
public:
virtual int foo() = 0;
};
class Bar : public Foo {
public:
int foo() { return Foo::foo(); }
};
int main() {
Bar bar;
bar.foo();
}
---------------------------------------------------------------------------
Tim Hollebeek | Disclaimer :=> Everything above is a true statement,
<space for rent> | for sufficiently false values of true.
Princeton Univ. | email: tim@wfn-shop.princeton.edu
-------------------| http://wfn-shop.princeton.edu/~tim (NEW! IMPROVED!)
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. Moderation policy:
http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]
Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: 1996/02/16 Raw View
tim@franck.Princeton.EDU (Tim Hollebeek) writes:
>Is the following legal C++ code? g++ compiles it, but it doesn't link
>(undefined function Foo::foo()).
No, the following code is not legal, because you don't have a definition
for the pure virtual function Foo::foo(). But if you add one, e.g.
int Foo::foo() { return 42; }
then the code is quite legal.
>I'm suprised it compiles at all:
It has always been legal in C++ to provide a definition for pure
virtual functions, although the only way to invoke such a function is
by using an explicit qualification.
Yes, this suprised me too ;-) I think it would have been simpler to
disallow definitions of pure virtual functions.
--
Fergus Henderson WWW: http://www.cs.mu.oz.au/~fjh
fjh@cs.mu.oz.au PGP: finger fjh@128.250.37.3
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. Moderation policy:
http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]