Re: error C2248: a derived class' f() can't access its base class protected member through a pointer?!
From: tom_usenet (tom_usenet_at_hotmail.com)
Date: 02/19/04
- Next message: tom_usenet: "Re: sizeof() operator"
- Previous message: Hendrik Schober: "Re: sizeof() operator"
- In reply to: David F: "Re: error C2248: a derived class' f() can't access its base class protected member through a pointer?!"
- Next in thread: Naanthaa N: "Re: error C2248: a derived class' f() can't access its base class protected member through a pointer?!"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 19 Feb 2004 12:17:33 +0000
On Sat, 14 Feb 2004 02:59:14 GMT, "David F"
<David-White@earthlink.net> wrote:
>Thanks Igor.
>
>So if that is the case, it means that the only "clean" solution is to go all
>the time through a public function of the base or contained class, since the
>other two options I mentioned are either undesirable (making the data
>members public) or unacceptable (declaring friends, and this is because when
>this class is designed, it does not know about the derived or containing
>classes that will come later).
The alternative is to (dynamic_)cast the pointer down to your derived
class, so you can access the members. If the function is only intended
to be passed derived objects, this is the right thing to do.
On the other hand, in many complicated cases
>and/or very large dynamic data containers, going through the function call
>may be prohibitively costly in run time.
>
>And if so, I think it was a mistake by the language designer not to
>compromise by making a better use of protected members of base or contained
>class by giving the protected members special direct access rights through
>pointers (limited only by properly qualified by type).
On the contrary, the rule is necessary to ensure that encapsulation is
maintained. Imagine:
class A
{
protected:
int i;
};
class C: public A
{
public:
C()
{
i = 1; //set invariant.
}
};
class B: public A
{
public:
void foo(A* a)
{
a->i = 10; //illegal (fortunately)
}
};
int main()
{
C c; //has invariant a==1
B b;
b.foo(&c); //breaks invariant!
}
The rule is there to stop you accessing the protected parts of other
classes derived from the same base. I've looked through the access
rules in C++ in some detail over the years, and there don't seem to be
any problems - they are entirely consistent and logical with regard to
pointers to members, members, bases, friends, etc., etc. (with the
possible exception of nested classes).
Tom
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
- Next message: tom_usenet: "Re: sizeof() operator"
- Previous message: Hendrik Schober: "Re: sizeof() operator"
- In reply to: David F: "Re: error C2248: a derived class' f() can't access its base class protected member through a pointer?!"
- Next in thread: Naanthaa N: "Re: error C2248: a derived class' f() can't access its base class protected member through a pointer?!"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|