Sign in to follow this  
deffer

mem_func and protected methods

Recommended Posts

deffer    754
Greetings. I was playing around with standard libraries, and so I came across for_each and the family. The context(simplified) is as follows:
class A
{
   friend class ManagerA;;
   // ...
protected:
   void Update();
};

class ManagerA
{
   // ...
   container<A*>  m_As;
   void UpdateAllAs(void);
};

void ManagerA::UpdateAllAs(void)
{
   // original solution.
   for (container<A*>::iterator it=m_As.begin(); it!=m_As.end(); ++it) {
      (*it)->Update();
   };
   // same using functors
   for_each(m_As.begin(), m_As.end(), std::mem_fun(A::Update));

   // but if the quest was a little more complicated,
   // it couldn't be simply coded without:
   m_As.for_each_on_edges(0, std::mem_fun(A::Update) );
};

container is a theoretical class, but I do really have a _method_ for_each_on_edges (for a 2d array). So when I try to use version with functors, compiler points that the functor cannot access the protected method A::Update. Cool, I understand, I did put the "protected:" there myself. But isn't it how it is done, by setting the manager class as a friend of A? for_each and family do not cooperate with this softly. It is clearly a design problem? How is it being solved, or how to avoid it? Thanks. /def

Share this post


Link to post
Share on other sites
gulgi    315
Hello...

class A is friend with ManagerA, not with any standard containers, therefor:
m_As.for_each_on_edges(0, std::mem_fun(A::Update) ); ...can not work.

If you write such a function you call it with:
for_each_on_edges(m_As.begin(), m_As.end(), 0, std::mem_fun(A::Update) );

Btw.. what does that function do, and do you really need your own function for that? an Update with some argument might be better? Or perhaps another std function?

Good luck!

Share this post


Link to post
Share on other sites
deffer    754
Quote:
Original post by gulgi
class A is friend with ManagerA, not with any standard containers, therefor:
m_As.for_each_on_edges(0, std::mem_fun(A::Update) ); ...can not work.

Actually, A should be a friend of the given functor for this to work. Now that cannot be achieved, since the functors are created and defined on-the-fly.

Quote:

If you write such a function you call it with:
for_each_on_edges(m_As.begin(), m_As.end(), 0, std::mem_fun(A::Update) );

Btw.. what does that function do


Heh, that's my own function ;) It "performs" the given functor on the objects from the edges of given 2d array container. But it's written in standard library form, only it's a method, not a function, but it really should make no difference.

Quote:

and do you really need your own function for that? an Update with some argument might be better? Or perhaps another std function?


The case was, that my code was bloated with

for (int x=0; x<width; ++x) {
for (int y=0; y<height; ++y) {
container[x][y]->DoSomething();
};
};

Or much worse, doing somewhat on the edges only, or internal(non-edges) only.
I would like to encapsulate this common piece of machinery.

Common std::for_each has the same problem, so the solution is not with adding functionality...

Cheers.
/def


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this