Re: Conversion operators and private inheritance
On 1 Feb., 06:34, Konstantin <k...@gmx.com> wrote:
Hello,
I`ve run into situation when conversion operator to the parent class
provided by the child class (private inheritance is used) is not found by
compiler. Consider the following example :
class A {};
class B : public A {};
class C : private B {
public:
// By default, "A" base class is not available for the
// outside non-friend classes and functions. Make it accessible
// for others
operator A& () {return *this;}};
void doSomething (const A&) {}
int main (int argc, char** argv){
C instC;
// Attempt 1, most obvious. causes compiler error
doSomething (instC.operator A&());
// Attempt 2. Causes compiler error
doSomething (instC);
// Attempt 3. Fine
doSomething (instC.C::operator A&());
return 0;
}
Compiler output (gcc 4.4.1) :
gcc_private_inheritance.cpp: In function ?int main(int, char**)?:
gcc_private_inheritance.cpp:2: error: ?class A? is inaccessible
gcc_private_inheritance.cpp:13: error: within this context
gcc_private_inheritance.cpp:15: error: ?A? is an inaccessible base of ?C
Let`s don`t discuss the sanity of this inheritance scheme / need for
conversion operator :)
So can anyone explain why conversion operator is not found in (1) and (2) ?
See [class.conv.fct]/1:
"A conversion function is never used to convert a (possibly
cv-qualified) object to the (possibly cv-qualified) same object
type (or a reference to it), to a (possibly cv-qualified) base
class of that type (or a reference to it), or to (possibly
cv-qualified) void.[Foot note 111]
111) These conversions are considered as standard
conversions for the purposes of overload resolution
(13.3.3.1, 13.3.3.1.4) and therefore initialization (8.5)
and explicit casts (5.2.9). A conversion to void does
not invoke any conversion function (5.2.9). Even
though never directly called to perform a conversion,
such conversion functions can be declared and can
potentially be reached through a call to a virtual
conversion function in a base class."
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]