Protected Inheritance question/problem

I have two classes:
1
2
3
4
5
6
7
8
9
class A {
protected:
virtual void render() { /*code*/ }
};
class B: public A {
protected:
A *aInstance;
virtual void 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
Last edited on
Nobody knows?
hi,
subclasses of A are able to call render() , but they can call it via scope resolution operator.
like this

A::render();

and not like this

aInstance -> render();

This actually means that you are calling a protected function outside of the class, which is not allowed!!!
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;
virtual void 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?
Last edited on
Can anyone help? this is eating away at my soul...
a n chatterjee is right. Maybe you can make the method public or use 'friend' to use this approach.
closed account (z05DSL3A)
Top of my head code structure
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Foo
{
public:
    Foo(){};
    friend class FooList;
protected:
    virtual void render() { /*code*/ }

};

class FooList : public std::list<Foo>
{
public:
    void render()
    { /* for each Foo in list call Foo.render*/ }
};

class Bar :public Foo
{
public:
   Bar(){};

   FooList aFooList;

protected:
   virtual void render() {aFooList.render(); };
};
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.

Good luck :)
Topic archived. No new replies allowed.