Hi,
Suppose I have a base class and a derived class as follows:
class base
{
public:
void somefunc ();
};
class derived : private base
{
public:
void someotherfunc ();
};
My intend is to prevent the user of 'derived' to access the members of
'base'. The problem is that nothing prevents the user to down-cast the
'derived' class to the 'base' class and then access that member. Is there a
solution to this, other than simply documenting it?
thanks!
--
jb
(replace y with x if you want to reply by e-mail) 10 2398
"Jakob Bieling" <ne*****@gmy.net> wrote in message news:bm*************@news.t-online.com... My intend is to prevent the user of 'derived' to access the members of 'base'. The problem is that nothing prevents the user to down-cast the 'derived' class to the 'base' class and then access that member. Is there a solution to this, other than simply documenting it?
The only way (safely) to down convert derived to base is to use a C-style cast. That's
the one cast the C-style cast will do that the C++ casts won't. In order to cast via a
C++ cast (or even implicitly convert) the base class would need to be accessible (i.e.,
not private). A reinterpret_cast is dangerous (there's no guarantee that the converted
pointer would be correct, and in the case of multiple inheritance it almost assuredly is
wrong for some of the bases).
Jakob Bieling wrote: Hi,
Suppose I have a base class and a derived class as follows:
class base { public: void somefunc (); };
class derived : private base { public: void someotherfunc (); };
My intend is to prevent the user of 'derived' to access the members of 'base'.
Why? The whole idea of inheritance is that a 'derived' can be used like
a 'base' and shares its interface. If you need another interface,
consider making derived a member of base.
The problem is that nothing prevents the user to down-cast the 'derived' class to the 'base' class and then access that member. Is there a solution to this, other than simply documenting it?
Again, make derived a member of base.
Rolf Magnus wrote: Jakob Bieling wrote:
Hi,
Suppose I have a base class and a derived class as follows:
class base { public: void somefunc (); };
class derived : private base { public: void someotherfunc (); };
My intend is to prevent the user of 'derived' to access the members of 'base'.
Why? The whole idea of inheritance is that a 'derived' can be used like a 'base' and shares its interface. If you need another interface, consider making derived a member of base.
The problem is that nothing prevents the user to down-cast the 'derived' class to the 'base' class and then access that member. Is there a solution to this, other than simply documenting it?
Again, make derived a member of base.
Grmbl. I just got used to not reading my postings anymore before sending
them, which is not a good idea. Of course I mean making base a member
of derived.
"Jakob Bieling" <ne*****@gmy.net> wrote in message
news:bm*************@news.t-online.com... class base { public: void somefunc (); };
class derived : private base { public: void someotherfunc (); };
My intend is to prevent the user of 'derived' to access the members of 'base'. The problem is that nothing prevents the user to down-cast the 'derived' class to the 'base' class and then access that member.
Users can't get access to private members simply by casting. What results
are you seeing that makes you think they can?
"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f*********************@news.newshosting.com. .. "Jakob Bieling" <ne*****@gmy.net> wrote in message
news:bm*************@news.t-online.com... My intend is to prevent the user of 'derived' to access the members
of 'base'. The problem is that nothing prevents the user to down-cast the 'derived' class to the 'base' class and then access that member. Is
there a solution to this, other than simply documenting it?
The only way (safely) to down convert derived to base is to use a C-style
cast. That's the one cast the C-style cast will do that the C++ casts won't. In order
to cast via a C++ cast (or even implicitly convert) the base class would need to be
accessible (i.e., not private). A reinterpret_cast is dangerous (there's no guarantee that
the converted pointer would be correct, and in the case of multiple inheritance it
almost assuredly is wrong for some of the bases).
Does this mean due to the 'private' inheritance, my compiler should flag
an error, unless I use a C-style cast?
--
jb
(replace y with x if you want to reply by e-mail)
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:bm*************@news.t-online.com... Jakob Bieling wrote:
Hi,
Suppose I have a base class and a derived class as follows:
class base { public: void somefunc (); };
class derived : private base { public: void someotherfunc (); };
My intend is to prevent the user of 'derived' to access the members of 'base'.
Why? The whole idea of inheritance is that a 'derived' can be used like a 'base' and shares its interface.
But the whole point of *private* inheritance is to hide the interface from
users of "derived".
"jeffc" <no****@nowhere.com> wrote in message
news:3f********@news1.prserv.net... "Jakob Bieling" <ne*****@gmy.net> wrote in message news:bm*************@news.t-online.com... class base { public: void somefunc (); };
class derived : private base { public: void someotherfunc (); };
My intend is to prevent the user of 'derived' to access the members
of 'base'. The problem is that nothing prevents the user to down-cast the 'derived' class to the 'base' class and then access that member.
Users can't get access to private members simply by casting. What results are you seeing that makes you think they can?
I thought that by casting the 'derived' object to a 'base' object, you
can then access the 'somefunc' member, which you would otherwise not be able
to access. But Ron's post shed some light on this, though I am still not
100% that I understood him right.
--
jb
(replace y with x if you want to reply by e-mail)
Hi,
I forgot to mention that I need to store both the 'base' objects and the
'derived' objects in one array (which is why I could not use Rolf's
suggestion). This also makes my question a pretty useless one, because when
storing the 'derived' pointers as 'base' pointers, I would do exactly what I
am trying to prevent. So now I came up with having a common base class to
both 'base' and 'derived' and do not let 'derived' derive from 'base' (now
the class names seem rather badly chosen).
It also prevents code duplication which I will get when finishing both
classes later. I should have thought a little more before posting, my
apologies.
Thanks for your time!
--
jb
(replace y with x if you want to reply by e-mail)
"Jakob Bieling" <ne*****@gmy.net> wrote in message news:<bm*************@news.t-online.com>... class base { public: void somefunc (); };
class derived : private base { public: void someotherfunc (); };
My intend is to prevent the user of 'derived' to access the members of 'base'. The problem is that nothing prevents the user to down-cast the 'derived' class to the 'base' class and then access that member. Is there a solution to this, other than simply documenting it?
Private inheritance prevents regular (non-friend) users from casting
between base and derived classes using static_cast or dynamic_cast. It
does *not* prevent them from using C-style cast notation to perform
such casts. However, any competent programmer should know not to use
C-style casts here (or just about anywhere, really).
Best practice: Use private inheritance and leave it at that. Do not
bother documenting how (not) to use C++ features that the user should
already know about, or that are beyond your control.
BTW: Casting from a subclass (derived class) to a superclass (base
class) is upcasting, not downcasting.
- Shane
"Shane Beasley" <sb******@cs.uic.edu> wrote in message
news:2f**************************@posting.google.c om... BTW: Casting from a subclass (derived class) to a superclass (base class) is upcasting, not downcasting.
Oh, using the words sub- and superclass it makes perfect sense, yes!
Thanks for the clarification!
--
jb
(replace y with x if you want to reply by e-mail) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Axel Dahmen |
last post by:
Hi,
when I'm programming style sheets for a whole web application I'm missing a
CSS feature to be able to reference CSS properties of other elements.
Let's say there is a...
|
by: Pedro |
last post by:
Hi, all.
I'm having a problem which vb developers had for certain al least
once.
I have my base form, declared as musthinherit;
Public MustInherit Class frmsdvPrincipal
|
by: Mike Tyka |
last post by:
Hello community,
i'm fairly new to using the STL but i've been experimenting a bit with it.
I tried to derive a new class say MyString from string like so:
class MyString: public string{...
|
by: Steve Jorgensen |
last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling
a particular case of implementation inheritance that would be easy and obvious
in a fully OOP language, but is not at...
|
by: Matthew Louden |
last post by:
I want to know why C# doesnt support multiple inheritance? But why we can
inherit multiple interfaces instead? I know this is the rule, but I dont
understand why. Can anyone give me some concrete...
|
by: Sinex |
last post by:
Hi,
Why does C# disallow multiple inheritance? Whats the reason behind this?
Is there any advantage or is it just a method to avoid some problems (if so,
what problems?) that come with multiple...
|
by: VR |
last post by:
Hi, I read about Master Pages in ASP.Net 2.0 and after implementing some
WinForms Visual Inheritance I tryed it with WebForms (let's say .aspx pages,
my MasterPage does not have a form tag itself...
|
by: Mark O'Flynn |
last post by:
I would like some advice regarding implementing inheritance using vb.net.
I have an application written in vb6 that I am rewritting from the ground up
in vb.net to take full advantage of .net,...
|
by: Mark |
last post by:
why doesn't .NET support multiple inheritance?
I think it's so silly!
Cheers,
Mark
|
by: Shawnk |
last post by:
Some Sr. colleges and I have had an on going discussion relative to when and
if
C# will ever support 'true' multiple inheritance.
Relevant to this, I wanted to query the C# community (the...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |