This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: GNU & Borland Pure Virtual Functions.


On 12-Feb-2001, Jason McDonald <jasonm@telefloratech.com> wrote:
> Why would GNU report an error with this code and Borland Builder would not?

GNU is following the ISO C++ standard.  Borland Builder probably has
the behaviour enabled by default for backwards compatibility with old
(pre-standard) code.  I suspect that if you enable strict standard
conforming mode for Borland Builder, then it will probably issue a 
diagnostic for it -- as any conforming ISO C++ compiler must.

The issue is not pure virtual functions, which are a red herring in
this example; the issue is passing a temporary to a function that
expects a non-const reference.

> #include <iostream.h>
> #include <string.h>
> 
> class A{
> public:
>     char temp[10];
>     virtual void from(char *s) { cout << "FROM A";}
>     virtual void blank(void) = 0;
>     virtual ~A(){};
> };
> 
> class B: public A{
> public:
> 
>     virtual void from(char *s) { strcpy(temp,s);}
>     virtual void blank(void) { cout << "BLANK B";}
>     virtual ~B(){};
> };
> 
> B doingB(char *s)
> {
>     B b;
>     b.from(s);
>     return b;
> }
> 
> void printit(A &a)
> {
>     cout << a.temp << " YADA\n";
> }
> 
> int main(void)
> {
>     printit(doingB("THIS"));
>     return 0;
> }
> 
> GNU error:
> >a.c: In function `int main ()':
> >a.c:34: could not convert `doingB()' to `A &'
> >a.c:28: in passing argument 1 of `printit (A &)'

Which version is that?
egcs 1.1.2 reports the following,

	bash$ g++ -ansi -pedantic aa.c
	aa.c: In function `int main()':
	aa.c:34: warning: initialization of non-const reference `class A &' from rvalue `B'
	aa.c:28: warning: in passing argument 1 of `printit(A &)'

and g++ 2.95 reports

	bash$ g++ -ansi -pedantic aa.c
	aa.c: In function `int main()':
	aa.c:36: initialization of non-const reference type `class A &'
	aa.c:36: from rvalue of type `B'
	aa.c:30: in passing argument 1 of `printit(A &)'

both of which seem a little clearer.

-- 
Fergus Henderson <fjh@cs.mu.oz.au>  |  "I have always known that the pursuit
                                    |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]