473,324 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

When is a base class protected member not visible in a derived class?

Can anyone tell me why the code below doesn't compile?

The code has a simple hierarchy of publically-derived classes: A -> B
-> C. A declares a protected member 'foo'. C declares an object of
type B, and attempts to access B.foo, which results in a compiler
error:

test.cpp: In member function `void C::test()':
test.cpp:5: error: `int A::foo' is protected
test.cpp:15: error: within this context

Cheers -

AL

----------------------------------------------------------
#include <iostream>

class A {
protected:
int foo;
};

class B: public A {
};

class C: public B {
public:
void test() {
B b;
std::cout << b.foo << std::endl;
}
};

int main() {
C c;
c.test();
}
----------------------------------------------------------
Jul 23 '05 #1
5 1949
Andy Lomax wrote:
Can anyone tell me why the code below doesn't compile?
See below.
The code has a simple hierarchy of publically-derived classes: A -> B
-> C. A declares a protected member 'foo'. C declares an object of
type B, and attempts to access B.foo, which results in a compiler
error:

test.cpp: In member function `void C::test()':
test.cpp:5: error: `int A::foo' is protected
test.cpp:15: error: within this context
Yes. The cause: a common misconception of what 'protected' is for.

Cheers -

AL

----------------------------------------------------------
#include <iostream>

class A {
protected:
int foo;
};

class B: public A {
};

class C: public B {
public:
void test() {
B b;
std::cout << b.foo << std::endl;
Inside a C object you're only allowed to access protected members of
_your_own_ instance (through this->) or of another instance of type C.
This should be OK:

this->foo;
C c;
c.foo;
}
};

int main() {
C c;
c.test();
}
----------------------------------------------------------


V
Jul 23 '05 #2

"Andy Lomax" <ab***@[127.0.0.1]> wrote in message
news:mp********************************@4ax.com...
Can anyone tell me why the code below doesn't compile?

The code has a simple hierarchy of publically-derived classes: A -> B
-> C. A declares a protected member 'foo'. C declares an object of
type B, and attempts to access B.foo, which results in a compiler
error:

test.cpp: In member function `void C::test()':
test.cpp:5: error: `int A::foo' is protected
test.cpp:15: error: within this context

Cheers -

AL

----------------------------------------------------------
#include <iostream>

class A {
protected:
int foo;
};

class B: public A {
};

class C: public B {
public:
void test() {
B b;
std::cout << b.foo << std::endl;
}
};

int main() {
C c;
c.test();
}
----------------------------------------------------------


As Victor said, protected is for internal access - if you want to have
external access like you have then you can use the 'friend' keyword.

Allan
Jul 23 '05 #3
On Wed, 29 Jun 2005 11:08:33 -0400, Victor Bazarov
<v.********@comAcast.net> wrote:
Andy Lomax wrote:
Can anyone tell me why the code below doesn't compile?


Yes. The cause: a common misconception of what 'protected' is for.


Great.

Thanks -

AL
Jul 23 '05 #4
Another way.

class C: public B {
public:
void test() {
std::cout << B::foo << std::endl;
}

Jul 23 '05 #5
"anthonyhan" <an********@msn.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com
Another way.

class C: public B {
public:
void test() {
std::cout << B::foo << std::endl;
}


That is equivalent to

class C: public B {
public:
void test() {
std::cout << foo << std::endl;
}

It accesses the protected member in the object's *own* B subobject and
doesn't address the original issue, which is accessing a protected member in
*another* instance of B.
--
John Carson

Jul 23 '05 #6

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

Similar topics

2
by: Michael Young | last post by:
I've been trying to compile code similar to the example code below, but I keep getting errors indicating that the function 'foo()' is not accessible. At first, I thought this was a bug in the...
4
by: Siemel Naran | last post by:
Can Derived class static member access protected member from base class? class Base { protected: void setvariable(int); }; class Derived : Base { public: static std::auto_ptr<Base> out(new...
8
by: LAvoisieR | last post by:
Following test code behaviour suprised me and I dont know what's wrong with this. I have two overloaded constructors within base class and virtual destructor implemented. Derived class uses...
6
by: Rick | last post by:
Hi, Can anyone explain to me why the below fails to compile - seeing otherA->f(); as a call to a inaccessible function, while otherB->f(); is ok? It seems you can happily access protected...
15
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
15
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:...
4
by: softwaredoug | last post by:
Here is some test code I've been attempting to compile (Visual Studio 2003) test.h: class Base { protected: Base() {} public:
8
by: Mayur H Chauhan | last post by:
All, For my knowledge, if I declare Class as follow, then it thows compilation error. Protected Class Book End Class Even same for...
3
by: Edan | last post by:
I have a base class with protected members (`Base`). The function `MakeBase()` is a member function of another class, that returns a `Base` object initialized with private members of this class. Now...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.