473,473 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Access to public base classes protected functions in objects differentfrom this

Can anyone provide a good explanation to why the following does not
work? After all since A is a public base of B objects of type B ISA
object of type A.

class A {
protected:
void foo() {}
};

class B : public A {
public:
void bar(A &a, B &b) {
foo(); //< OK, accessing A::foo in *this
b.foo(); //< OK, accessing A::foo in other object of type B
a.foo(); //< ERROR: foo() is protected in this context
}
}

/Daniel
Jul 22 '05 #1
5 1455

"Daniel Aarno" <ma*******@users.sf.net> wrote in message
Can anyone provide a good explanation to why the following does not
work? After all since A is a public base of B objects of type B ISA
object of type A.
Protected members are accessible to objects of same type, or a type derived
from the accessing class.
class A {
protected:
void foo() {}
};

class B : public A {
public:
void bar(A &a, B &b) {
foo(); //< OK, accessing A::foo in *this
b.foo(); //< OK, accessing A::foo in other object of type B
a.foo(); //< ERROR: foo() is protected in this context
Here type A is not the same as type B (from where you are accessing) and is
also NOT a type derived from B.
}
}


Sharad
Jul 22 '05 #2
* Daniel Aarno:

Can anyone provide a good explanation to why the following does not
work? After all since A is a public base of B objects of type B ISA
object of type A.

class A {
protected:
void foo() {}
};

class B : public A {
public:
void bar(A &a, B &b) {
foo(); //< OK, accessing A::foo in *this
b.foo(); //< OK, accessing A::foo in other object of type B
a.foo(); //< ERROR: foo() is protected in this context
}
}


It's not 100% clear-cut, but a practical language design decision.

Consider a class B2 also deriving from A, and

class B: public A
{
void bar( A& a, B2 &b2 )
{
}
}
Why shouldn't code in class B be able to access the protected member
in 'b2'?

Well, we don't know that that's safe, in any way. In class B2 it might
be a requirement that the object is in a certain state before the
call. Or it might be that some further action is required after the
call to bring 'b2' to a valid external state; the function is protected
precisely because its usage requires knowledge of and access to the
the inner workings of the object, which is the opposite of encapsulation.

Now consider your original example, but called with an object of class C
as actual argument for 'b', where class C is derived from B. Again the
code in class B doesn't know whether its safe to call the protected
member function, because class C might have imposed additional requirements.
The reason this is not 100% clear-cut is that class B might have as part
of its contract that no derived class should change the requirements.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #3
Sharad Kala wrote:
"Daniel Aarno" <ma*******@users.sf.net> wrote in message

Can anyone provide a good explanation to why the following does not
work? After all since A is a public base of B objects of type B ISA
object of type A.

Protected members are accessible to objects of same type, or a type derived
from the accessing class.


Isn't B the accessing class? I don't think A::foo would be accessible from a
class C derived from the accessing class B.

Looking in The C++ programming language (Stroustrup S.E 2001) it sez the
follosing about a protected member:

"...its name can be used only by member functions and friends of the class in
which it is declared and by member functions and friends of classes derived
from this class."

This would make me think that:
a.foo();
b.foo();
should either booth fail or both succede

Anyone have any insight to what exactly the standard sez about this?
class A {
protected:
void foo() {}
};

class B : public A {
public:
void bar(A &a, B &b) {
foo(); //< OK, accessing A::foo in *this
b.foo(); //< OK, accessing A::foo in other object of type B

a.foo(); //< ERROR: foo() is protected in this context

Here type A is not the same as type B (from where you are accessing) and is
also NOT a type derived from B.
}
}

Sharad

Jul 22 '05 #4

"Daniel Aarno" <ma*******@users.sf.net> wrote in message
news:ck**********@inn.nada.kth.se...
Sharad Kala wrote:
"Daniel Aarno" <ma*******@users.sf.net> wrote in message

Can anyone provide a good explanation to why the following does not
work? After all since A is a public base of B objects of type B ISA
object of type A.

Protected members are accessible to objects of same type, or a type derived from the accessing class.


Isn't B the accessing class? I don't think A::foo would be accessible

from a class C derived from the accessing class B.


B is the accessing class but the pointer is of type A*.

There are two requirements for protected access - the accessing class must
be the same as or derived from the class containing the protected member AND
the pointer must be the same as or derived from the accessing class.

The second requirement is often overlooked.

John
Jul 22 '05 #5
* Alf P. Steinbach:
* Daniel Aarno:

Can anyone provide a good explanation to why the following does not
work? After all since A is a public base of B objects of type B ISA
object of type A.

class A {
protected:
void foo() {}
};

class B : public A {
public:
void bar(A &a, B &b) {
foo(); //< OK, accessing A::foo in *this
b.foo(); //< OK, accessing A::foo in other object of type B
a.foo(); //< ERROR: foo() is protected in this context
}
}


It's not 100% clear-cut, but a practical language design decision.

Consider a class B2 also deriving from A, and

class B: public A
{
void bar( A& a, B2 &b2 )
{
}
}
Why shouldn't code in class B be able to access the protected member
in 'b2'?

Well, we don't know that that's safe, in any way. In class B2 it might
be a requirement that the object is in a certain state before the
call. Or it might be that some further action is required after the
call to bring 'b2' to a valid external state; the function is protected
precisely because its usage requires knowledge of and access to the
the inner workings of the object, which is the opposite of encapsulation.

Now consider your original example, but called with an object of class C
as actual argument for 'b', where class C is derived from B. Again the
code in class B doesn't know whether its safe to call the protected
member function, because class C might have imposed additional requirements.
The reason this is not 100% clear-cut is that class B might have as part
of its contract that no derived class should change the requirements.


Uhm, I wrote something dumb here -- please ignore.

Cheers,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #6

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

Similar topics

2
by: Kevin Saff | last post by:
Apparently I'm missing something. Stroustrup (15.3) says of protected access: If is protected, its name can be used only by member functions and friends of the class in which it is declared...
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...
11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
9
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
7
by: Andy Ward | last post by:
Given the following code: class A { protected: int pro; }; class B : public A { public:
12
by: Manolis | last post by:
Hi, I was wondering if there is any way to make two objects of the same class to be able to access each other's private data, like this: class A { public: void access( const A& a )...
3
by: Nathan Wiegman | last post by:
Hi, Why must derived classes access protected base members through an instance of a derived class? The following code would work in C++, why doesn't it in C#? public class A { protected...
3
by: Samuel Burri | last post by:
Hi there I got a simple problem. As you can see in the posted source, I have two classes, where one is derived from the other. In fact, they also contain virtual functions. In the function...
86
by: jopperdepopper | last post by:
Hi, finally giving php 5 a go, and going over the new approach to classes. Can someone clarify the public, private and protected to me? I quote the php manual: "The visibility of a property or...
0
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
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
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
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
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
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.