Spectral Spectral - 1 month ago 18
C++ Question

C++ virtual + protected?

In C++, I have a base class A, a sub class B. Both have the virtual method Visit.
I would like to redefine 'Visit' in B, but B need to access the 'Visit' function of each A (and all subclass to).

I have something like that, but it tell me that B cannot access the protected member of A! But B is a A too :-P

So, what can I do?

class A
{
protected:
virtual Visit(...);
}

class B : public class A
{
protected:
vector<A*> childs;
Visit(...);
}

B::Visit(...)
{
foreach(A* a in childs)
{
a->Visit(...);
}
}


Thx

Answer

It is corrected in the "example". Now B is subclass of A.

Comments