🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

3 total messages Started by Tim Hollebeek Fri, 16 Feb 1996 04:15
Legal C++ code?
#3465
Author: Tim Hollebeek
Date: Fri, 16 Feb 1996 04:15
27 lines
1404 bytes
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. ]
Re: Legal C++ code?
#3468
Author: Fergus Henderson
Date: Fri, 16 Feb 1996 04:39
28 lines
1581 bytes
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. ]
Re: Legal C++ code?
#3362
Author: Don Wallace
Date: Tue, 20 Feb 1996 15:44
42 lines
1753 bytes
tim@franck.Princeton.EDU (Tim Hollebeek) wrote:

>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();
>}

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.

- Don


[ 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
]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads