Cast to protected base fails even with explicit operator

R

RR

I'm sure this has been answered before but two hours of searching and
reading hasn't answered this question for me.

Here's a test program:
********************************
class base
{
public:
};
class der : protected base
{
public:
der()
{
}
der(char* x,const base& thing)
{
}
};
main()
{
der d1;
der d2("xx",d1);
}
********************************

The GCC compiler rightly says:
x.cpp: In function `int main ()':
x.cpp:21: fields of `base' are inaccessible in `der' due to private
inheritance

But, now I add the following methods to "der":

operator base&()
{
return (*this);
}
operator const base&() const
{
return (*this);
}

and the compiler still complains with the same error.

I've given 'der' explicit conversion operator overloads and the compiler
doesn't seem to see them.

Can some explain this, please?

TIA
 
R

RR

BTW, please respond to the group as my email address will discard anything
you send to it.
 
J

John Harrison

I'm sure this has been answered before but two hours of searching and
reading hasn't answered this question for me.

Here's a test program:
********************************
class base
{
public:
};
class der : protected base
{
public:
der()
{
}
der(char* x,const base& thing)
{
}
};
main()
{
der d1;
der d2("xx",d1);
}
********************************

The GCC compiler rightly says:
x.cpp: In function `int main ()':
x.cpp:21: fields of `base' are inaccessible in `der' due to private
inheritance

But, now I add the following methods to "der":

operator base&()
{
return (*this);
}
operator const base&() const
{
return (*this);
}

and the compiler still complains with the same error.

I've given 'der' explicit conversion operator overloads and the compiler
doesn't seem to see them.

Can some explain this, please?

TIA

You not getting any answer from the gurus so I'll give it my best shot.

You have a derived object and a base class reference. In that case no
conversion operators will be applied. The fact that the base class is
inaccessible is irrelevant.

You are looking for some rule that says something like 'if the base class
is inaccessible then try possible conversions' but no such rule exists.

A similar situation that is found in other circumstances as well. For
instance when deciding which overloaded member function to call,
accessibility is *not* considered. The compiler considers which overloaded
member function to call and then checks if it is accessible. If it isn't
it issues an error, it doen't go back and try to find the second best
member function to call.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top