Topic: Defect? Virtual function overriding ignores cv-qualification
Author: dmh0xq001@sneakemail.com (Alan Stokes)
Date: Thu, 12 Sep 2002 18:31:29 +0000 (UTC) Raw View
10.3/2 says:
"If a virtual member function vf is declared in a class Base and in a
class Derived, derived directly or indirectly from Base, a member
function vf with the same name and same parameter list as Base::vf is
declared, then Derived::vf ... overrides Base::vf."
Consider
struct B
{
virtual void f() const;
};
struct D : B
{
virtual void f();
};
void g()
{
const D d;
const B & b = d;
b.f();
}
According to the letter of the standard, D::f should override B::f
(same name, same parameter list). But that allows g to invoke a
non-const function on a const object without using a const_cast, which
is a bad thing.
I suspect that there should be a requirement that the cv-qualification
of the functions should match too. (Unless this requirement is
somewhere else, of course - but I have looked and failed to find it.)
- Alan
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: vAbazarov@dAnai.com ("Victor Bazarov")
Date: Thu, 12 Sep 2002 19:44:13 +0000 (UTC) Raw View
"Alan Stokes" <dmh0xq001@sneakemail.com> wrote...
> 10.3/2 says:
>
> "If a virtual member function vf is declared in a class Base and in a
> class Derived, derived directly or indirectly from Base, a member
> function vf with the same name and same parameter list as Base::vf is
> declared, then Derived::vf ... overrides Base::vf."
>
> Consider
>
> struct B
> {
> virtual void f() const;
> };
> struct D : B
> {
> virtual void f();
> };
>
> void g()
> {
> const D d;
> const B & b = d;
> b.f();
> }
>
> According to the letter of the standard, D::f should override B::f
> (same name, same parameter list).
You're forgetting the "hidden" argument -- the pointer to the
object. 'const' qualifier after the function relates to _that_
parameter. So, accroding to the letter of the Standard, D::f
will not override, but instead will _hide_ the B::f.
> But that allows g to invoke a
> non-const function on a const object without using a const_cast, which
> is a bad thing.
>
> I suspect that there should be a requirement that the cv-qualification
> of the functions should match too. (Unless this requirement is
> somewhere else, of course - but I have looked and failed to find it.)
The "hidden" argument is considered part of the parameter list.
See 1.3.10.
Victor
--
Please remove capital A's from my address when replying by mail
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: hyrosen@mail.com (Hyman Rosen)
Date: Thu, 12 Sep 2002 19:44:18 +0000 (UTC) Raw View
Alan Stokes wrote:
> According to the letter of the standard, D::f should override B::f
> (same name, same parameter list). But that allows g to invoke a
> non-const function on a const object without using a const_cast, which
> is a bad thing.
I think 13.1/2 should be invoked to define what "same parameter list"
means, and that section states that the type of the implicit object
parameter counts.
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: dmh0xq001@sneakemail.com (Alan Stokes)
Date: Fri, 13 Sep 2002 16:15:23 +0000 (UTC) Raw View
vAbazarov@dAnai.com ("Victor Bazarov") wrote in message news:<uo1omnjsqlp10@corp.supernews.com>...
> You're forgetting the "hidden" argument -- the pointer to the
> object. 'const' qualifier after the function relates to _that_
> parameter. So, accroding to the letter of the Standard, D::f
> will not override, but instead will _hide_ the B::f.
I'm not forgetting it - but it isn't part of the parameter list. (And
if it was you could never have an overriding function, because the
hidden parameter would be B * in one case and D * in the other, so the
parameter lists could never be the same.)
> The "hidden" argument is considered part of the parameter list.
> See 1.3.10.
That says the cv-qualification of the function is part of the
signature. But 10.3/3 specifically talks about parameter lists not
signatures, so I don't think 1.3.10 is relevant.
And replacing "the same parameter list" with "the same signature"
doesn't work, because the signature includes the class containing the
method, which as above is always going to be different in a would-be
overriding declaration.
- Alan
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]