473,241 Members | 1,446 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

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

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
Jul 19 '05 #1
3 2485

"Philippe Guglielmetti" <ne**@dynabits.com> wrote in message news:3f**********************@news.sunrise.ch...
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"; }};
^

Jul 19 '05 #2
"Philippe Guglielmetti" <ne**@dynabits.com> 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

Jul 19 '05 #3
"Ron Natalie" <ro*@sensor.com> wrote...

"Philippe Guglielmetti" <ne**@dynabits.com> wrote in message news:3f**********************@news.sunrise.ch...
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

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
18
by: Erik Arner | last post by:
Hi, I really need some help here. After upgrading to g++ 3.4 I have run into all sorts of troubles that I'm sure depends on my lack of proper understanding of C++. I would now like to get it right...
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
15
by: Susan Baker | last post by:
Hello everybody, I'm new to C++ (I have some C background). I've read up on this topic a few times but it just dosen't seem to be sinking in. 1. Whats the difference between overloading and...
5
by: Tony Johansson | last post by:
Hello experts! Why is not possible to have virtual static members Many thnakn //Tony
4
by: Rafael Veronezi | last post by:
I have some questions about override in inheritance, and virtual members. I know that you can you override a method by two ways in C#, one, is overriding with the new keyword, like: public new...
1
by: Joel | last post by:
Why does this work: using System; namespace ConsoleApplication1 { class Class1 { static void Main(string args)
4
by: Aamir Mahmood | last post by:
Hi all, I have a question. Can static members (methods and nested types) be made virtual in a class? So that they can be overridden in the child classes. For example: -------------------...
8
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.