class A {
protected:
virtualvoid render() { /*code*/ }
};
class B: public A {
protected:
A *aInstance;
virtualvoid render() { aInstance->render(); /*more code*/ }
};
I feel like subclasses of A should be able to call render() on an A* object, but i'm getting a compiler error in my code. Am I missing something? Is this really not allowed? Do i really need to:
A - make render() public -or-
B - declare subclasses friend classes of A
So what you're saying is i can call A::render() on the instance of B that i'm currently in, but i can't call render() on another instance of my class?
in reality i have a list of A pointers instead of one-
1 2 3 4 5 6 7 8 9 10 11
class B: public A {
protected:
std::list<A*> displayList;
virtualvoid render() {
/*render me*/
for(/*each (it) in displayList*/) {
//Render my children
(**it).render();
}
}
};
i'm calling a protected function from outside of the class instance, but inside the class heirarchy. In my book that should be legal. (and in Java/others it is)
I would like to give derived classes of A the ability to access information about instances of A (functions and variables), and at the same time restrict access to this information from classes outside of the heirarchy. Is this possible?
First, friend is a bad option for this. A friendly access is all about granting access to all of its private, public properties of the grantor to grantee class, which is not that you want.
And about your question basically, accessing the methods of parents class from inherited (derived) class but not from public object's scope.
The 'protected' scope is meant for that only and what you are doing for its declaration is right.
And about a pointer to parent's instance is not clear.
When the derived class is given access to parent's protected methods and data, why would you like to have another instance for the same?
Whe you have access to your dad's bank account why would you ask your dad to create another intermediary account to access his account.
You already have the access, why would you need another intermediary one?
I am not clear. May be I could help you if you could provide more details of the problem/work you want to achive.