[comp.lang.c++] Virtual functions under cfront 1.21 and g++ 1.37.1

ecsv38@castle.ed.ac.uk (S Manoharan) (06/08/90)

Consider the following code:

#include <stream.h>

class A {
private:
   int x;
protected:
   virtual void printx()        { cout << x << "\n"; }
public:
   A(int x1)                    { x = x1; }
   void hello()                 { printx(); }
};

class B: public A {
protected:
   virtual void printx()        { cout << "gee, naa\n"; }
public:
   B(int x1) : (x1)             { }
   void greet()                 { printx(); }
};

class C: public B {
protected:
   virtual void printx(int x)   { cout << x << "\n"; }
public:
   C(int x1) : (x1)             { }
   void bye()                   { printx(3); }
};  // line 27

int main()
{
   A a(2); B b(4); C c(8);

   a.hello();
   b.greet();
   c.bye();

   return 0;
}

----
<<cfront 1.2.1 2/16/87>> produces the error:

"prot.c", line 27: error: virtual C::printx() type mismatch: 
void C::(int ) and void B::()

----
g++ version 1.37.1 compiles fine and produces this result:
2
gee, naa
3
----

I then moved the virtuals to the public area. I had the same error
under cfront. And same result under g++.

What is my code supposed to do? Is this a bug in g++?

Thanks.

-- 
S. Manoharan               Janet: sam@uk.ac.ed.lfcs
Dept of Computer Science   Uucp : ..!mcsun!ukc!edcastle!lfcs!sam
University of Edinburgh    Arpa : sam%lfcs.ed.ac.uk@nsfnet-relay.ac.uk
Edinburgh EH9 3JZ    UK.   Voice: 667 8323(home) 667 1018x2708(office)

randolph@ektools.UUCP (Gary L. Randolph) (06/11/90)

In article <4566@castle.ed.ac.uk> ecsv38@castle.ed.ac.uk (S Manoharan) writes:
>Consider the following code:
OK
>class A {
>private:
>   int x;
>protected:
>   virtual void printx()        { cout << x << "\n"; }
    ...stuff deleted...
>};

>class B: public A {
>protected:
>   virtual void printx()        { cout << "gee, naa\n"; }
    ...stuff deleted...
>};

>class C: public B {
>protected:
>   virtual void printx(int x)   { cout << x << "\n"; }

    Oops.  virtual functions must have identical signatures and
    return types.

    Lippman (Pg. 342)
    Dewhurst & Stark (Pg. 113)

    ...stuff deleted...
>};  // line 27

    main() deleted...

><<cfront 1.2.1 2/16/87>> produces the error:

    As it should
>"prot.c", line 27: error: virtual C::printx() type mismatch: 
>void C::(int ) and void B::()

----
>g++ version 1.37.1 compiles fine and produces this result:
>2
>gee, naa
>3

>What is my code supposed to do?
     What it did :-)

>Is this a bug in g++?
     No.  g++ has several extensions.  If you do not require
     that your code be portable to cfront then use them but
     be aware that they are extensions.

Gary
@
@
@
@
@
@
@
@
@
@
@

pkturner@cup.portal.com (Prescott K Turner) (06/13/90)

S. Manoharan writes:

[Cfront 1.2.1 indicates an error.]
> g++ version 1.37.1 compiles fine 

> What is my code supposed to do? Is this a bug in g++?

This is a bug in your cfront.  Cfront 2.0 gets the same results as g++,
with the following message:
    line 23: warning:  C::printx() hides virtual B::printx()
--
Prescott K. Turner, Jr.
Language Processors, Inc.
959 Concord St., Framingham, MA 01701 USA    (508) 626-0006 x232
UUCP: ...sun!cup.portal.com!pkturner    Internet: pkturner@cup.portal.com