473,770 Members | 2,217 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 1804
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************ ****@spamgourme t.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
1678
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 pieces of data around, and requires that certain methods return an Envelope with a specific kind of Letter as it's content. I have a cast operator that converts from what I've got to what should be returned, but it seems that the compiler only...
17
2677
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 cboFont_DrawItem event handler? Why does it need to be cast? -hazz ,................. cboFont.Items.AddRange(FontFamily.Families); } private void cboFont_DrawItem(object sender,
3
21221
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 obj=Activator.CreateInstance(strAssemblyName, "MyClass"); //Now, I want to do something like ((MyClass)obj).Method //Can I do this?
5
3437
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 5), but fails on IIS 6 running on a Win2003 server. The web uses Pages derived from a custom class I wrote (which itself derives from Page) to provide some common functionality. The Page_Load handler the failing webpage starts out like this: ...
6
6729
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
2658
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: ------------------ public class One : IOne { .... }//One public class Two {
17
2517
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
2
1766
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
3126
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
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10102
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
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 we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.