Topic: Inheritance Problems


Author: "Chris Newton" <chrisnewton@no.junk.please.btinternet.com>
Date: Mon, 14 May 2001 17:42:08 GMT
Raw View
Stuart Golodetz <sgolodetz@dial.pipex.com> wrote...
> Can a B also access another B's private members? Cos
> before I read this I worked on the assumption that it
> was only on a per-object basis...

Yes. (So says 11.2/4 of the standard.)

This idea is often used when writing, e.g., copy constructors and copy
assignment operators.

Cheers,
Chris


---
[ 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                ]





Author: Karl Heinz Buchegger <kbuchegg@gascad.at>
Date: Mon, 14 May 2001 22:55:34 GMT
Raw View

Stuart Golodetz wrote:
>
> Can a B also access another B's private members?

Yes, it can. One B object can access *all* members of
another B object. Otherwise writing cctor's and assignment
operators would become a pain in the ass.

> Cos before I read this I
> worked on the assumption that it was only on a per-object basis...
>
As Chris said: access is checked on a per-class base.

--
Karl Heinz Buchegger
kbuchegg@gascad.at

---
[ 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                ]





Author: James Dennett <jdennett@acm.org>
Date: Thu, 10 May 2001 23:41:35 GMT
Raw View
"O.Petzold" wrote:
>
> Hello,
>
> the follwoing code isn't working (with the gcc-2.95.2).
> If I see the inharritance it should !
>
> class A {
> public:
>   void ModifyAnother(A* a2) {
>     b = 1;              // is working
>     a2->b = 1;          // woking as well
>   }
> protected:
>   int b;
> };
>
>
> class B : public A {
> public:
>   void ModifyAnother(A* a2)  {
>     b = 1;              // is working
>     a2->b = 1;          // not working
>   }
> };
>
> int main() {
>     B b;
> }
>
> `int A::b' is protected
>
> Well but, B is derived from A !!
> What says the standard about ?

That objects of class B have access to protected parts
of objects that are themselves of class B, not to any
other objects which are A's (or instances of subclasses
of A which aren't subclasses of B).

-- James Dennett

---
[ 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                ]





Author: "Chris Newton" <chrisnewton@no.junk.please.btinternet.com>
Date: Sat, 12 May 2001 15:28:04 GMT
Raw View
O.Petzold <opetzold@wit.regiocom.net> wrote...
> the follwoing code isn't working (with the gcc-2.95.2).
> If I see the inharritance it should !
>
> class A {
> public:
>   void ModifyAnother(A* a2) {
>     b = 1;              // is working
>     a2->b = 1;          // woking as well
>   }
> protected:
>   int b;
> };
>
>
> class B : public A {
> public:
>   void ModifyAnother(A* a2)  {
>     b = 1;              // is working
>     a2->b = 1;          // not working
>   }
> };
>
> int main() {
>     B b;
> }
>
> `int A::b' is protected
>
> Well but, B is derived from A !!
> What says the standard about ?

Your compiler is correct. You can only access a protected member of A in
B via a pointer or reference to a B object. An A* isn't good enough.
This is from 11.5/1 in the standard if you want to check.

If you replaced B::ModifyAnother's parameter with (B* a2) instead, it
should then work, because member access is controlled on a per-class and
not per-object basis, so this B can access another B's protected
members.

Hope that helps,
Chris


---
[ 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                ]





Author: "David Rijsman" <drijsmanNO_SPAM@hotmail.com>
Date: Sat, 12 May 2001 15:30:29 GMT
Raw View
O.Petzold <opetzold@wit.regiocom.net> wrote in message
news:3AFA4600.7656A271@wit.regiocom.net...
> Hello,
>
> the follwoing code isn't working (with the gcc-2.95.2).
> If I see the inharritance it should !
>
> class A {
> public:
>   void ModifyAnother(A* a2) {
>     b = 1;              // is working
>     a2->b = 1;          // woking as well
>   }
> protected:
>   int b;
> };
>
>
> class B : public A {
> public:
>   void ModifyAnother(A* a2)  {
>     b = 1;              // is working
>     a2->b = 1;          // not working
>   }
> };
>
> int main() {
>     B b;
> }
>
> `int A::b' is protected
>
> Well but, B is derived from A !!
> What says the standard about ?
>
> Thanks
> Olaf
>
> ---
> [ 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                ]
>
>

As you might know you can make class B a friend of class A by adding:

class A {
friend class B;
 public:
   void ModifyAnother(A* a2) {
     b = 1;              // is working
     a2->b = 1;          // woking as well
   }
 protected:
   int b;
};

this will do what you are trying;

David



---
[ 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                ]





Author: "Stuart Golodetz" <sgolodetz@dial.pipex.com>
Date: Mon, 14 May 2001 11:17:24 GMT
Raw View
"Chris Newton" <chrisnewton@no.junk.please.btinternet.com> wrote in message
news:9dh57t$uj$1@uranium.btinternet.com...
> O.Petzold <opetzold@wit.regiocom.net> wrote...
> > the follwoing code isn't working (with the gcc-2.95.2).
> > If I see the inharritance it should !
> >
> > class A {
> > public:
> >   void ModifyAnother(A* a2) {
> >     b = 1;              // is working
> >     a2->b = 1;          // woking as well
> >   }
> > protected:
> >   int b;
> > };
> >
> >
> > class B : public A {
> > public:
> >   void ModifyAnother(A* a2)  {
> >     b = 1;              // is working
> >     a2->b = 1;          // not working
> >   }
> > };
> >
> > int main() {
> >     B b;
> > }
> >
> > `int A::b' is protected
> >
> > Well but, B is derived from A !!
> > What says the standard about ?
>
> Your compiler is correct. You can only access a protected member of A in
> B via a pointer or reference to a B object. An A* isn't good enough.
> This is from 11.5/1 in the standard if you want to check.
>
> If you replaced B::ModifyAnother's parameter with (B* a2) instead, it
> should then work, because member access is controlled on a per-class and
> not per-object basis, so this B can access another B's protected
> members.

Can a B also access another B's private members? Cos before I read this I
worked on the assumption that it was only on a per-object basis...

Stuart.


---
[ 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                ]





Author: "O.Petzold" <opetzold@wit.regiocom.net>
Date: Thu, 10 May 2001 17:48:58 GMT
Raw View
Hello,

the follwoing code isn't working (with the gcc-2.95.2).
If I see the inharritance it should !

class A {
public:
  void ModifyAnother(A* a2) {
    b = 1;              // is working
    a2->b = 1;          // woking as well
  }
protected:
  int b;
};


class B : public A {
public:
  void ModifyAnother(A* a2)  {
    b = 1;              // is working
    a2->b = 1;          // not working
  }
};

int main() {
    B b;
}

`int A::b' is protected

Well but, B is derived from A !!
What says the standard about ?

Thanks
Olaf

---
[ 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                ]





Author: "M. Kools" <mkools@euronet.nl>
Date: Thu, 10 May 2001 18:06:06 GMT
Raw View
"O.Petzold" <opetzold@wit.regiocom.net> schreef in bericht
news:3AFA4600.7656A271@wit.regiocom.net...
> Hello,
>
> the follwoing code isn't working (with the gcc-2.95.2).
> If I see the inharritance it should !
>
> class A {
> public:
>   void ModifyAnother(A* a2) {
>     b = 1;              // is working
>     a2->b = 1;          // woking as well
>   }
> protected:
>   int b;
> };
>
>
> class B : public A {
> public:
>   void ModifyAnother(A* a2)  {
>     b = 1;              // is working
>     a2->b = 1;          // not working
>   }
> };
>
> int main() {
>     B b;
> }
>
> `int A::b' is protected
>
> Well but, B is derived from A !!
> What says the standard about ?
>
> Thanks
> Olaf
>

you're making a pointer to a new object, and this can't access the variable
b cuz it's protected.


---
[ 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                ]