Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is this valid C++ (protected member of base accessed in derived class function)

Reply
Thread Tools

Is this valid C++ (protected member of base accessed in derived class function)

 
 
Shelly Adhikari
Guest
Posts: n/a
 
      09-10-2003
class B {
public:
B() { }
~B() { }
protected:
int abc;
void g() {}
};

class C : public B {
public:
void f(B* p) {
p->g(); // Should this be an error?
int m = p->abc; // Should this be an error?
int n = abc;
g();
}
};

int main() {
B* y = new B();
C x;
x.f(y);
}

If you could also point me to the ISO C++ standard section and page
number, it would be great.
 
Reply With Quote
 
 
 
 
=?iso-8859-1?Q?Juli=E1n?= Albo
Guest
Posts: n/a
 
      09-10-2003
Shelly Adhikari escribió:

> class B {
> public:
> B() { }
> ~B() { }
> protected:
> int abc;
> void g() {}
> };
>
> class C : public B {
> public:
> void f(B* p) {
> p->g(); // Should this be an error?
> int m = p->abc; // Should this be an error?


Yes, they are errors. You have access only to a the B that is part of a
C, not to a unrelated B.

Regards.
 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      09-10-2003

"Shelly Adhikari" <(E-Mail Removed)> wrote in message
> p->g(); // Should this be an error?
> int m = p->abc; // Should this be an error?

Yes, these are both errors.
> int n = abc;
> g();

These are OK.

Protected only lets you get at your own instances (this's) subobjects.


 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      09-10-2003
Shelly Adhikari wrote:
> [protected member access question redacted]


To whoever maintains the FAQs:

Is this one in the FAQs? It's been asked at least five times in the past week. If it isn't,
it should be in there.


 
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
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
very intersting:derived class private member accessed??? Bangalore C++ 3 06-23-2006 03:40 PM
When is a base class protected member not visible in a derived class? Andy Lomax C++ 5 06-30-2005 04:34 AM
Can Derived class static member access protected member from base class? Siemel Naran C++ 4 01-12-2005 06:46 PM



Advertisments