Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > overriding static and virtual members with same proto. What is the standard ?

Reply
Thread Tools

overriding static and virtual members with same proto. What is the standard ?

 
 
Philippe Guglielmetti
Guest
Posts: n/a
 
      10-22-2003
Look at these few lines of code:

class A { public: virtual void f() { cout << "A";}};
class B : public A{public: static void f() { cout << "B"; }};
class C : public B{public: void f() { cout << "C"; }}; // virtual or not ?
that's the question...

int main(int, char**)
{
A* x; A a; B b; C c;
a.f(); b.f(); c.f();
x = &a; x->f(); x = &b; x->f(); x = &c; x->f();
}

Borland C++ 5.5 outputs "ABCAAA"
MS VC6 and 7 give "ABCAAC"

my explanation is that C::f overrides the virtual A::f with VC,
while C::f overrides the static B::f with BC5. In fact, if C is redefined as

class C : public B{public: virtual void f() { cout << "C"; }};

then BC5 behaves as VC

Questions:
1) what does the standard say? (I was unable to find a clear answer..)
2) what is the reason for allowing the above code to compile without even a
warning ?

--
Philippe Guglielmetti - www.dynabits.com


 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      10-22-2003

"Philippe Guglielmetti" <(E-Mail Removed)> wrote in message news:3f9699a8$0$3677$(E-Mail Removed)...
> Look at these few lines of code:
>
> class A { public: virtual void f() { cout << "A";}};
> class B : public A{public: static void f() { cout << "B"; }};
> class C : public B{public: void f() { cout << "C"; }}; // virtual or not ?
> that's the question...


Both compilers are wrong. The program is ILL-FORMED. The
rules say that a member function that is the same name and parameters
as a virtual function in the base is also virtual (10.3/2) and static
members shall NOT be declared virtual (9.4.1 / 2)

Comeau catches the erorr

"ComeauTest.c", line 2: error: only nonstatic member functions may be virtual
class B : public A{public: static void f() { cout << "B"; }};
^



 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-22-2003
"Philippe Guglielmetti" <(E-Mail Removed)> wrote...
> Look at these few lines of code:
>
> class A { public: virtual void f() { cout << "A";}};
> class B : public A{public: static void f() { cout << "B"; }};
> class C : public B{public: void f() { cout << "C"; }}; // virtual or not ?
> that's the question...
>
> int main(int, char**)
> {
> A* x; A a; B b; C c;
> a.f(); b.f(); c.f();
> x = &a; x->f(); x = &b; x->f(); x = &c; x->f();
> }
>
> Borland C++ 5.5 outputs "ABCAAA"
> MS VC6 and 7 give "ABCAAC"
>
> my explanation is that C::f overrides the virtual A::f with VC,


Yes (10.3/2).

> while C::f overrides the static B::f with BC5.


That's, I am sorry, nonsense. It's an apparent bug in BC5.

> In fact, if C is redefined as
>
> class C : public B{public: virtual void f() { cout << "C"; }};
>
> then BC5 behaves as VC


That should be of no difference. Whether 'f' is declared virtual
or not in C, it should be considered overriding A::f _simply_because_
they have the same signature and A::f is virtual.

>
> Questions:
> 1) what does the standard say? (I was unable to find a clear answer..)


B::f hides A::f. If you try to access 'f' using a reference to B
or a pointer to B, the compiler should use B::f (10.2/2).

C::f in turn hides B::f _and_ overrides A::f. That means that if
you access 'f' using a reference to A or a pointer to A, but the
object is really a C, the C::f should be called.

> 2) what is the reason for allowing the above code to compile without even

a
> warning ?


There is no requirement in the standard to issue any diagnostic
when compiling a well-formed program, IIRC.

Victor



 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      10-22-2003
"Ron Natalie" <(E-Mail Removed)> wrote...
>
> "Philippe Guglielmetti" <(E-Mail Removed)> wrote in message

news:3f9699a8$0$3677$(E-Mail Removed)...
> > Look at these few lines of code:
> >
> > class A { public: virtual void f() { cout << "A";}};
> > class B : public A{public: static void f() { cout << "B"; }};
> > class C : public B{public: void f() { cout << "C"; }}; // virtual or not

?
> > that's the question...

>
> Both compilers are wrong. The program is ILL-FORMED. The
> rules say that a member function that is the same name and parameters
> as a virtual function in the base is also virtual (10.3/2) and static
> members shall NOT be declared virtual (9.4.1 / 2)
>
> Comeau catches the erorr
>
> "ComeauTest.c", line 2: error: only nonstatic member functions may be

virtual
> class B : public A{public: static void f() { cout << "B"; }};


How about that!

I always thought that static and non-static members had different
number of arguments due to the hidden argument, the "this" pointer.
You made me look more carefully at this and I discovered 13.1 where
the Standard says that names cannot be overloaded if the only
difference between them is that one is static and the other is not.

HOWEVER... B::f does not _overload_ A::f, it _hides_ it (different
scope).

Now, I cannot find a direct confirmation in the Standard of either
point of view. If you could elaborate on your quotes (for all I
know you 9.4.1/2 refers to the fact that 'virtual' and 'static'
cannot appear in the same member function declaration), I'd really
appreciate it.

It would be nice to hear from Greg Comeau as well.

Victor



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Order of destruction of static members and static objects Victor Bazarov C++ 11 12-09-2009 08:54 AM
Difference between static final members and final static members(if any)? JFCM Java 4 02-07-2006 11:32 AM
About static const members appearing in another static const definitions Rakesh Sinha C++ 4 01-13-2005 08:11 AM
Instantiating a static class( Class with all static members - methods and variables) SaravanaKumar Java 6 10-19-2004 08:20 AM
Static classes with static members Ben ASP .Net 3 06-01-2004 07:43 PM



Advertisments