Board index » cppbuilder » Overloading virtual functions

Overloading virtual functions


2004-12-20 07:45:26 PM
cppbuilder95
Has anyone noticed that before - while under visual c++ (.net)
overloading virtual functions works in compliance with c++, under
borland cppbuilder (bcb5) fail to do so. Care to look at the following
example:
class A{
public:
virtual void f(int a){printf("A::int\n");}
virtual void f(){printf("A::void\n");}
};
class B: public A{
public:
using A::f;
void f(){printf("B::void\n");}
};
class C: public B{
public:
using B::f;
void f(int a){printf("C::int\n");}
};
int main()
{
C c;
A& ar=c;
c.f(1);
ar.f(1);
return 0;
}
BCB shows two compilation warnings (W8022 'function1' hides virtual
function 'function2') and produces the wrong output:
C::int
A::int
Visual c++ produces the correct output:
C::int
C::int
Same code, different outputs - has this been recognized as a bcb bug,
or else? I cound not find anything like this in the groups... thanks
for replying.
regards,
Saxo
 
 

Re:Overloading virtual functions

Looks like Borland incorrectly deduces static type of ar as A.
Both compilers can call the virtual functions directly, because they know
exact type of the object 'c'. What they do with 'ar' calls may depend on the
optimizer. It is possible to deduce actual type of 'ar', but Borland seems
to make an error.
< XXXX@XXXXX.COM >wrote in message
Quote
Has anyone noticed that before - while under visual c++ (.net)
overloading virtual functions works in compliance with c++, under
borland cppbuilder (bcb5) fail to do so. Care to look at the following
example:

class A{
public:
virtual void f(int a){printf("A::int\n");}
virtual void f(){printf("A::void\n");}
};

class B: public A{
public:
using A::f;
void f(){printf("B::void\n");}
};

class C: public B{
public:
using B::f;
void f(int a){printf("C::int\n");}
};

int main()
{
C c;
A& ar=c;
c.f(1);
ar.f(1);
return 0;
}

BCB shows two compilation warnings (W8022 'function1' hides virtual
function 'function2') and produces the wrong output:
C::int
A::int

Visual c++ produces the correct output:
C::int
C::int

Same code, different outputs - has this been recognized as a bcb bug,
or else? I cound not find anything like this in the groups... thanks
for replying.


regards,
Saxo

 

Re:Overloading virtual functions

I don't think so - the mess-up (and warning) comes within classes
definitions itself not their instantiation. BCB simply doesn't handle
correctly the very combination of overloading-scope mechanism (together
with using-declaration) and virtualness of functions - something gets
screwed up in putting them together...
Ok, this looks like an obvious and overlooked bug to me, which persist
into BCB6 version. Surely, not many programmers use these tools of
language, but nevertheless, if you want to use them, you must take
Visual or some other compiler.
saxo
 

{smallsort}