Re: Calling protected base class function on other (same typed) object
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/