Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Shelly Adhikari
Guest
 
Posts: n/a
#1: Jul 19 '05
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.

Julián Albo
Guest
 
Posts: n/a
#2: Jul 19 '05

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


Shelly Adhikari escribió:
[color=blue]
> 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?[/color]

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

Regards.
Ron Natalie
Guest
 
Posts: n/a
#3: Jul 19 '05

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



"Shelly Adhikari" <adhikarishelly@yahoo.com> wrote in message[color=blue]
> p->g(); // Should this be an error?
> int m = p->abc; // Should this be an error?[/color]
Yes, these are both errors.[color=blue]
> int n = abc;
> g();[/color]
These are OK.

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


red floyd
Guest
 
Posts: n/a
#4: Jul 19 '05

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


Shelly Adhikari wrote:[color=blue]
> [protected member access question redacted][/color]

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.


Closed Thread