Re: taking address of protected base member function not possible?? what to do?
Frank Birbacher wrote:
Hi!
I have a class hierachy whose objects at runtime make up a tree
structure. There are two important classes in this hierachy. First
there is a common base for all classes. Second there is a derived
"container base" class which provides basic container features such as
owning children. In fact there are more classes, but I omitted them
for this post.
struct Base {...}; // 1
struct ContainerBase : Base {...}; // 2
Within this tree I want to enumerate all objects in a pre-order tree
walk. The Base class has an "unsigned id" attribute which is to be
assigned numbers starting from 1. I introduced a virtual method in
Base which is to be redefined in derived classes according to their
children. This method is declared protected because the user shall not
call it directly.
That last sentence is why your code does not work.
As far as the children of ContainerBase are concerned, ContainerBase is
nothing more than a user. Therefore, the protected access specifier
prevents you from calling assignIDs on the children of ContainerBase.
You can declare ContainerBase as a friend of Base to indicate it is
trusted user.
struct Base
{
friend struct ContainerBase;
virtual ~Base() {}
unsigned getID() const { return id; }
protected:
unsigned id;
virtual void assignIDs(unsigned &counter) =0;
};
<snip>
Frank
Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://c-faq.com/
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]