By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,241 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

Cast to protected base fails even with explicit operator

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
RR
BTW, please respond to the group as my email address will discard anything
you send to it.

Jul 22 '05 #2

P: n/a
On Mon, 09 Aug 2004 05:08:43 GMT, RR <ne****************@spamgourmet.com>
wrote:
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
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.