Method overloading with const/non-const variants (was: Re: KDE/kdelibs/kdecore)
Friedrich W. H. Kossebau
kossebau at kde.org
Tue Jul 8 14:08:16 BST 2008
Am Dienstag, 8. Juli 2008, um 14:55 Uhr, schrieb Thiago Macieira:
> Friedrich W. H. Kossebau wrote:
> >What about overloaded methods? Hm, running the test from below shows me
> > that I have been thinking wrong for many years, duh. So the compiler
> > chooses the variant by the constness of the object? I thought it takes
> > all possible variants and then narrows the selection further by the
> > LHS, going for constness if possible?
> >Is this defined by the C++ specs? Does anyone know the rationale behind
> > this?
> Yes, this is defined by the C++ spec. When you call a function foo(...)
> with a set of parameters, the compiler will try to find all instances
> of "foo" in the class and see which one fits best to the parameters you
> The CV qualifier to a function applies to the this pointer implicit
> parameter. So you can think of that as:
> void foo(Class *this);
> void foo(const Class *this);
> void foo(volatile Class *this);
> void foo(const volatile Class *this);
> So now you can apply the same rules as parameter overloading. That's why
> the compiler chooses the less constraining one. That's also why
> the "const" variants may be called in any circumstances, but the
> non-const ones cannot be called with a const object.
Okay, understandable. Thanks for the explanation, Thiago.
Seems I got caught by wishful thinking, human being I am :)
Okteta - KDE Hex Editor, coming to you with KDE 4.1
More information about the kde-core-devel