473,499 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cast to protected base fails even with explicit operator

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
2 1781
RR
BTW, please respond to the group as my email address will discard anything
you send to it.

Jul 22 '05 #2
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1659
by: A. W. Dunstan | last post by:
I'm porting some code to Visual C++ and have run into a problem - the compiler won't use a user-written cast operator. The code uses an envelope-letter approach to passing (potentially) large...
17
2659
by: Hazz | last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender' line of code need to be there in this event handler? Isn't cboFont passed via the managed heap, not the stack, into this...
3
21198
by: mra | last post by:
I want to cast an object that I have created from a typename to the corresponding type. Can anycone tell me how to do this? Example: //Here, Create the object of type "MyClass" object...
5
3392
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
6
6708
by: Lore Leunoeg | last post by:
Hello I derived a class MyControl from the Control class. Public Class MyControl Inherits Control Sub New() MyBase.New() End Sub End Class
2
2644
by: John Richardson | last post by:
How does a custom casting operator work? I have a class that defines one, but doesn't work as expected (or perhaps, the way I want it to). I have the following example classes:...
17
2466
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
2
1751
by: fred | last post by:
Hi, Why should I do a cast (IOfferSetter) in the constructor of the Test class? Thanks. Fred public interface IOfferSetter { object Offer { set;} } public class OfferCtx {
18
3086
by: mark | last post by:
class SORef {...}; class SelectedFORef : public SORef {...}; SOFORef SOCastToSOF(SORef sor) { SelectedFORef sof=nil; sof=dynamic_cast<SelectedFORef>(sor); return sof; };
0
7012
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7225
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7392
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4920
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4605
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3101
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.