Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > about base class's protected member

Reply
Thread Tools

about base class's protected member

 
 
Zealot
Guest
Posts: n/a
 
      09-19-2004
class Base
{
protected:
int var;
};

class ChildB : public Base
{};

class ChildA : public Base
{
void foo( ChildB* B )
{
var = B->var; C2248..
}
};

why I can not process ChildB's member var ??


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      09-19-2004

"Zealot" <> wrote in message
news:cijcan$18oh$...
> class Base
> {
> protected:
> int var;
> };
>
> class ChildB : public Base
> {};
>
> class ChildA : public Base
> {
> void foo( ChildB* B )
> {
> var = B->var; C2248..
> }
> };
>
> why I can not process ChildB's member var ??
>


Because ChildB is not derived from ChildA. Access to protected members from
a derived class must be made through a pointer to, reference to, or object
of the derived class itself (or any class derived from that class).

Using your previous code for Base and ChildB

class ChildC : public ChildA {};

class ChildA : public Base
{
void foo(ChildA* a, ChildB* b ChildC* c)
{
a->var; // fine
b->var; // error
c->var; // fine
}
};

john



 
Reply With Quote
 
 
 
 
=?koi8-r?B?4c7B1M/Mycog+8nSz8vP1w==?=
Guest
Posts: n/a
 
      09-19-2004
Because only derived class members has rights to access protected members of a base class :

class base
{
protected:
int v;
};
class der : public base
{
public:
void foo(der *other)
{
this->v = 10; // ok
other->v = 10; // error
}
};
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      09-19-2004

"áÎÁÔÏÌÉÊ ûÉÒÏËÏ×" <> wrote in message
news:...
> Because only derived class members has rights to access protected members
> of a base class :
>
> class base
> {
> protected:
> int v;
> };
> class der : public base
> {
> public:
> void foo(der *other)
> {
> this->v = 10; // ok
> other->v = 10; // error
> }
> };


No, that isn't correct. Try compiling it, no compile errors.

john


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't take pointer-to-member of protected member of base class K. Frank C++ 8 03-22-2012 06:50 PM
Can Derived class static member access protected member from base class? Siemel Naran C++ 4 01-12-2005 06:46 PM
Access to protected base member Andy Ward C++ 7 06-21-2004 02:56 AM
Is this right? Can't call protected member of base class from derivedclass method, for another object Asfand Yar Qazi C++ 17 09-12-2003 04:44 PM
Is this valid C++ (protected member of base accessed in derived class function) Shelly Adhikari C++ 3 09-10-2003 08:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57