Topic: An explicit access violater?
Author: xnews.public.home@bogus.rtij.nl (Martijn Lievaart)
Date: 2000/11/15 Raw View
garyh@zipperint.com (Gary Hinger) wrote in
<3a1191aa$1_2@news.nwlink.com>:
>I can think of a legitimate use for a "violating_cast". You may have a
>class defined in a commonly used header file and you may want to print
>out a private member from outside of the class to aid you in debugging.
>The proper way to do this would be to add an accessor that returns the
>private member, but that might make for a long compile. If you want an
>answer quickly, just use a volating_cast. volating_cast falls in the
>category of "naughty" keywords along with const_cast, reinterpret_cast,
>and mutable.
>
I wouldn't call mutable naughty, on the contary! I find it very useful in
writing const correct code. F.i.
class DatabaseRoots
{
mutable Dict *dict;
DB *db;
public:
DatabaseRoots(DB *theDB) : db(theDB), dict(0) {}
DBRoot getRoot(string) const;
};
DBRoot DatabaseRoots::getRoot(string name) const
{
if (!dict)
dict = loadRoots(db);
return dict->find(name);
}
Many other legitimate uses exist. Mutable can be abused, as can any
language feature. But its intended use is to maintain logical constness
where physical constness will not do. As such, it does a good job and
should be in any serious programmers design-toolbox.
HTH,
M4
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: dwalker07@snet.net.invalid (Daryle Walker)
Date: 2000/11/15 Raw View
<wmm@fastdial.net> wrote:
[SNIP]
> No, something like this:
>
> class B { };
> class D: private B { };
> void f(D* dp) {
> B* bp1 = dp; // ill-formed: access violation
> B* bp2 = (B*) dp; // okay, explicit cast
> }
Yes, this is the kind of example I saw a while back. Since it is not
covered by a static_cast, const_cast, or reinterpret_cast, this use of
the C-style cast should get an explicit C++-style cast marker.
--
Daryle Walker
Mac, Internet, and Video Game Junkie
dwalker07 AT snet DOT net
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: David R Tribble <david@tribble.com>
Date: 2000/11/16 Raw View
Daryle Walker wrote:
> I've heard (in this newsgroup, I think) that one thing that the C-cast
> can do that the C++ casts cannot is to violate access (e.g. "private")
> specifiers in finding a member.
This is not true.
All old-style C casts can be rewritten as a series of one or more
new-type C++ casts.
> Maybe we could make this explicit with a "violating_cast"?
Shudder. Not another one, please.
--
David R. Tribble, mailto:david@tribble.com, http://david.tribble.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Ron Natalie <ron@sensor.com>
Date: 2000/11/16 Raw View
David R Tribble wrote:
>
> Daryle Walker wrote:
> > I've heard (in this newsgroup, I think) that one thing that the C-cast
> > can do that the C++ casts cannot is to violate access (e.g. "private")
> > specifiers in finding a member.
>
> This is not true.
I think what he was saying is you can cast to an inaccessible base
class with a C style cast. There is no C++ cast that will do this.
>
> All old-style C casts can be rewritten as a series of one or more
> new-type C++ casts.
>
That's not true either. Read paragraph 7 of 5.4.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: dwalker07@snet.net.invalid (Daryle Walker)
Date: 2000/11/14 Raw View
I've heard (in this newsgroup, I think) that one thing that the C-cast
can do that the C++ casts cannot is to violate access (e.g. "private")
specifiers in finding a member. Maybe we could make this explicit with
a "violating_cast"?
--
Daryle Walker
Mac, Internet, and Video Game Junkie
dwalker07 AT snet DOT net
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: James Dennett <james@evtechnology.com>
Date: 2000/11/14 Raw View
Daryle Walker wrote:
>
> I've heard (in this newsgroup, I think) that one thing that the C-cast
> can do that the C++ casts cannot is to violate access (e.g. "private")
> specifiers in finding a member. Maybe we could make this explicit with
> a "violating_cast"?
As I can't think of any valid use for such a "violating_cast," why should
it be supported? The C-style cast is there for backwards compatibility,
and in an ideal world would disappear one day.
Out of interest, can anybody tell us _why_ the C-style cast should be
allowed to gain access to private/protected base classes? It surely can't
be for C compatibility.
-- James Dennett <jdennett@acm.org>
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: wmm@fastdial.net
Date: 2000/11/14 Raw View
In article <3A117410.B6BC91FB@evtechnology.com>,
James Dennett <james@evtechnology.com> wrote:
> Out of interest, can anybody tell us _why_ the C-style cast should be
> allowed to gain access to private/protected base classes? It surely
can't
> be for C compatibility.
The use of a cast to allow conversion of a pointer-to-derived
to a pointer-to-private-base goes all the way back to the
earliest days of C++ (it's in the first edition of C++PL, 1986).
My guess is that it was viewed in the same spirit as other
casts that let you do dangerous things, like downcasting,
converting ints to pointers, etc. In other words, access
control was viewed as a safety mechanism that sometimes needed
to be subverted, like type safety, and the presence of an
explicit cast was the flag used to identify such expressions as
suspect.
Compatibility extends to old C++ also, not just to C.
--
William M. Miller, wmm@fastdial.net
Vignette Corporation (www.vignette.com)
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: "Gary Hinger" <garyh@zipperint.com>
Date: 2000/11/14 Raw View
I can think of a legitimate use for a "violating_cast". You may have a class
defined in a commonly used header file and you may want to print out a
private member from outside of the class to aid you in debugging. The proper
way to do this would be to add an accessor that returns the private member,
but that might make for a long compile. If you want an answer quickly, just
use a volating_cast. volating_cast falls in the category of "naughty"
keywords along with const_cast, reinterpret_cast, and mutable.
By the way, how is it that one can use a C-style cast to bypass the private
keyword? Are you talking about something like this?
class C {int x;} c;
*(int*)&c = 3;
struct S {int x;}
((S*)&c)->x = 4;
These are just reinterpret_casts and I believe they are unreliable in theory
since the compiler may insert arbitrary padding into the classes/structs.
James Dennett <james@evtechnology.com> wrote in message
news:3A117410.B6BC91FB@evtechnology.com...
> Daryle Walker wrote:
> >
> > I've heard (in this newsgroup, I think) that one thing that the C-cast
> > can do that the C++ casts cannot is to violate access (e.g. "private")
> > specifiers in finding a member. Maybe we could make this explicit with
> > a "violating_cast"?
>
> As I can't think of any valid use for such a "violating_cast," why should
> it be supported? The C-style cast is there for backwards compatibility,
> and in an ideal world would disappear one day.
>
> Out of interest, can anybody tell us _why_ the C-style cast should be
> allowed to gain access to private/protected base classes? It surely can't
> be for C compatibility.
>
> -- James Dennett <jdennett@acm.org>
>
> ---
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
> [ Note that the FAQ URL has changed! Please update your bookmarks. ]
>
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: wmm@fastdial.net
Date: 2000/11/14 Raw View
In article <3a1191aa$1_2@news.nwlink.com>,
"Gary Hinger" <garyh@zipperint.com> wrote:
> By the way, how is it that one can use a C-style cast to bypass the
private
> keyword? Are you talking about something like this?
>
> class C {int x;} c;
> *(int*)&c = 3;
> struct S {int x;}
> ((S*)&c)->x = 4;
No, something like this:
class B { };
class D: private B { };
void f(D* dp) {
B* bp1 = dp; // ill-formed: access violation
B* bp2 = (B*) dp; // okay, explicit cast
}
--
William M. Miller, wmm@fastdial.net
Vignette Corporation (www.vignette.com)
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]