Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Calling protected base class function on other (same typed) object

Reply
Thread Tools

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

 
 
Rick
Guest
Posts: n/a
 
      02-06-2007
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(); };
};

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-06-2007
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


 
Reply With Quote
 
 
 
 
Rick
Guest
Posts: n/a
 
      02-06-2007
On Feb 6, 3:56 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-07-2007
Rick wrote:
> On Feb 6, 3:56 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
>> 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


 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      02-07-2007
* 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?
 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      02-10-2007
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/


 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      02-11-2007

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/


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can a sub-class (composite object) member function access private or protected members of base class? pkpatil@gmail.com C++ 2 06-09-2006 03:09 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
calling virtual function results in calling function of base class... Andreas Lagemann C++ 8 01-10-2005 11:03 PM
calling virtual function results in calling function of base class ... tiwy C++ 0 01-09-2005 11:17 PM



Advertisments