473,396 Members | 1,942 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,396 software developers and data experts.

Calling protected base class function on other (same typed) object

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 functions of another (same
type) - but not via a base class pointer.... I've checked the FAQs,
Meyers etc but nothing obvious I can find explains it.

I wanted to implement a recursion down a tree (via parent/child base
class pointers) and keep the virtual functions protected - but this
behaviour seems to force me to make them public.

Any info would be very helpful.

Thanks

Rick
class A
{
protected:
virtual void f() const {};
};

class B : public A
{
protected:
virtual void f() const
{
A::f(); // OK

// OK
B * otherB = new B();
otherB->f();

// Not OK - f() protected....
A * otherA = new B();
otherA->f(); };
};

Feb 6 '07 #1
6 4120
Rick wrote:
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 functions of another (same
type) - but not via a base class pointer.... I've checked the FAQs,
Meyers etc but nothing obvious I can find explains it.
Access to protected members is allowed only through the object of
the same type. It's a limitation imposed by the language.
I wanted to implement a recursion down a tree (via parent/child base
class pointers) and keep the virtual functions protected - but this
behaviour seems to force me to make them public.
Consider the Visitor pattern. Keep virtual functions private and
have a public non-virtual function in the base class which will call
your virtual function. Or, make the visiting type a friend and keep
everything private.
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 6 '07 #2
On Feb 6, 3:56 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Rick wrote:
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 functions of another (same
type) - but not via a base class pointer.... I've checked the FAQs,
Meyers etc but nothing obvious I can find explains it.

Access to protected members is allowed only through the object of
the same type. It's a limitation imposed by the language.
I guess this is a static versus dynamic binding issue? The access
specifiers having to be checked at compile time...?
I wanted to implement a recursion down a tree (via parent/child base
class pointers) and keep the virtual functions protected - but this
behaviour seems to force me to make them public.

Consider the Visitor pattern. Keep virtual functions private and
have a public non-virtual function in the base class which will call
your virtual function. Or, make the visiting type a friend and keep
everything private.
Looks like a good solution.

Thanks for the advice.

Rick

Feb 6 '07 #3
Rick wrote:
On Feb 6, 3:56 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>Rick wrote:
>>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 functions of another (same
type) - but not via a base class pointer.... I've checked the FAQs,
Meyers etc but nothing obvious I can find explains it.

Access to protected members is allowed only through the object of
the same type. It's a limitation imposed by the language.
I guess this is a static versus dynamic binding issue? The access
specifiers having to be checked at compile time...?
I know that the rule of allowing access to protected functions only
through an object of the same type stems from the fact that you're
not supposed to be able to use it across a hierarchy. Example:

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

class B : public A {
public:
void bar(B& b) {
b.foo(); // OK
}
};

class C : public A {
public:
void bar(A& a) {
a.foo(); // NOT OK! Line 42
}
};

int main() {
B b;
C c;
c.bar(b);
}

If a call on line 42 were allowed, then you could call a protected
member function for an object (b) which has no relation to you (C).
I am not sure why it is bad, nothing immediately comes to mind.

[..]

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 7 '07 #4
* Victor Bazarov:
I am not sure why it is bad, nothing immediately comes to mind.
If it were allowed you could access any protected member of any class
simply by deriving from that class.

As it is, you need at least a cast when using the derive-from-it method.

--
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?
Feb 7 '07 #5
Rick wrote:
>>
It seems you can happily access protected functions of another (same
type) - but not via a base class pointer.... I've checked the FAQs,
Meyers etc but nothing obvious I can find explains it.

Access to protected members is allowed only through the object of
the same type. It's a limitation imposed by the language.
I guess this is a static versus dynamic binding issue? The access
specifiers having to be checked at compile time...?
The "protected" keyword allows to access to base class members of own
object, not base class members of other objects. Other objects of the same
class is exception due to copy and assign wants the access, but the
protected access to members of other objects of the same class is better to
use only for copy and assign purpose.

If you want to access to protected for other objects, use "friend".

--
Maksim A. Polyanin

"In thi world of fairy tales rolls are liked olso"
/Gnume/
Feb 10 '07 #6

Grizlyk wrote:
>>>
It seems you can happily access protected functions of another (same
type) - but not via a base class pointer.... I've checked the FAQs,
Meyers etc but nothing obvious I can find explains it.

Access to protected members is allowed only through the object of
the same type. It's a limitation imposed by the language.
I guess this is a static versus dynamic binding issue? The access
specifiers having to be checked at compile time...?

The "protected" keyword allows to access to base class members of own
object, not base class members of other objects. Other objects of the same
class is exception due to copy and assign wants the access, but the
protected access to members of other objects of the same class is better
to use only for copy and assign purpose.

If you want to access to protected for other objects, use "friend".
I have recallected how can access to other object. In the case of two
non-inherited different classes we can declare one as frient to other

class B;
class A{ friend class B; int i; };

but derived from other (derived from from B) will lose friend acssess, so
friend is not inherited, and if we wants to get access to one from derived
from other, we need to declare the access interface in other (in B).

class B
{
protected:
int& i(A& a)const {return a.i;}
};

class derived_B: public B
{
protected:
print(cosnt int);

public:
print(A& a){print( i(a) );}
};

By analogy, we need to declare in base forwarding interface to access from
derived from base to other base

template<class T>
class A
{
//logical protected interface
protected:
T member;

//protected access interface
//to logical protected interface
protected:
T& get_member(A& a)const{return a.member;}
};

template<class T>
class B: public A<T>
{
public:

void test()
{
// Not OK - f() protected....
//A * otherA = new B();
//otherA->f(); };

A<T *otherA = new B;
T tmp( get_member(*otherA) );
}
};

--
Maksim A. Polyanin

"In thi world of fairy tales rolls are liked olso"
/Gnume/
Feb 11 '07 #7

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

Similar topics

8
by: Andreas Lagemann | last post by:
Hi, after browsing FAQ and archive for a while I decided that the following is a legal question. Consider this: Class Base { public: Base() {}
13
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. ...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
12
by: st_ev_fe | last post by:
I've noticed that when constructing a subclass, the base class get's it's contructors called. Is there some way to avoid this? The base class has one variable, which gets initialised to 0. ...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
12
by: Peter Cranz | last post by:
hello, I've got the following problem: I have a construct similar like this: namespace A { class X {
2
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
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:...
6
by: Me | last post by:
I need to be able to acces non-virtual members of sublcasses via a base class pointer...and without the need for an explicit type cast. I thought a pure virtual getPtr() that acts as a type cast...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...

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.