Topic: virtual function overriding
Author: Oleg Zabluda <zabluda@math.psu.edu>
Date: 1998/05/19 Raw View
Alexandre Oliva <oliva@dcc.unicamp.br> wrote:
: John R MacMillan <john@mail.interlog.com> writes:
: > class Base { public: virtual void f(const int i); };
: > class D1 : public Base { public: void f(const int i); };
: > class D2: public D1 { public: void f(int i); };
: > Which f() gets called?
: D2::f, your interpretation is correct. But you're right, there's a
: bit of room for misinterpretation.
Some C and C++ compilers traditionally have problems with
top-level cv-qualifiers. For example, they would not let you do this:
void f(int);
void f(const int) {}
or will think that these are a declaration and definition of
two different functions, or something like that.
Those kinds of compilers, of course, will get confused over
the matter of overriding as well.
Oleg.
--
Life is a sexually transmitted, 100% lethal disease.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: john@mail.interlog.com (John R MacMillan)
Date: 1998/05/14 Raw View
Given the snippet of code:
class Base {
public:
virtual void f(const int i) { cout << "Base: " << i << endl; }
void g(const int i) { f(i); }
};
class D1 : public Base {
public:
void f(const int i) { cout << "D1: " << i << endl; }
};
class D2: public D1 {
public:
void f(int i) { cout << "D2: " << i << endl; }
};
and:
Base *bp = new D2;
bp->g(42);
Which f() gets called?
My reading of the draft standard suggests D2::f(), but I can see room
for argument, and I have two compilers one of which calls D2::f() and
the other of which calls D1::f().
The room for argument I see is that [class.virtual] says `with the
same name and _same_ parameter list' (emphasis mine) and in a foot-
note refers to [over]. In [over] the word `equivalent' is used
instead of same, and [over.load] says `Parameter declarations that
differ only in the presence or absence of const and/or volatile are
equivalent'.
So if `same' is equivalent to `equivalent', as I assume it to be,
then D2::f() overrides, and is the final overrider. Obviously one of
the compilers disagrees. Who's right?
--
To reply by mail, please remove "mail." from my address -- but please
send e-mail or post, not both
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1998/05/16 Raw View
John R MacMillan <john@mail.interlog.com> writes:
> class Base { public: virtual void f(const int i); };
> class D1 : public Base { public: void f(const int i); };
> class D2: public D1 { public: void f(int i); };
> Which f() gets called?
D2::f, your interpretation is correct. But you're right, there's a
bit of room for misinterpretation.
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]