473,509 Members | 3,095 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.
Jul 19 '05 #1
3 2133
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.
Jul 19 '05 #2

"Shelly Adhikari" <ad************@yahoo.com> 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.
Jul 19 '05 #3
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.
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2472
by: Jian H. Li | last post by:
Hello, What's the essential differences between the two ways of "class::member" & "object.member"(or object_pointer->member)? class C{ public: void f() {} int i; };
2
2270
by: Steven T. Hatton | last post by:
I find the surprising. If I derive Rectangle from Point, I can access the members of Point inherited by Rectangle _IF_ they are actually members of a Rectangle. If I have a member of type Point...
28
3381
by: Act | last post by:
Why is it suggested to not define data members as "protected"? Thanks for help!
8
19137
by: H. S. | last post by:
I am getting this error if I try to compile the file demarcated below. What I am missing here? I am using g++ (GCC) 3.3.5 (Debian 1:3.3.5-8). {tp2}> g++ -ansi -g -Wall tp2.cc -o tp2 tp2.cc: In...
13
7671
by: Adam H. Peterson | last post by:
I just made an observation and I wondered if it's generally known (or if I'm missing something). My observation is that static protected members are essentially useless, only a hint to the user. ...
3
2669
by: Bangalore | last post by:
Hi, In the following program, eventhogh two member function declared under private section of the derived class are accessable by derived class pointer. Please clarify me how can derived class...
16
3598
by: Fir5tSight | last post by:
Hi All, I have a small C#.NET program that is as follows: using System; class A { protected int x = 123; }
2
1924
by: t | last post by:
Lippman's C++ Primer, 4th ed., p562, dicussion of protected members seems to be wrong, unless I am misinterpreting things. He says: "A derived class object may access the protected members of...
15
3046
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I met with a strange issue that derived class function can not access base class's protected member. Do you know why? Here is the error message and code. error C2248:...
10
4048
by: blangela | last post by:
If I pass a base class object by reference (likely does not make a difference here that it is passed by reference) as a parameter to a derived class member function, the member function is not...
0
7234
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7136
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7344
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7412
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5652
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4730
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
441
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.