How to give a class a method X to do on some objects at some point?

Started by
5 comments, last by Waterlimon 11 years, 5 months ago
I have a class, that might need to go thru some objects at some point, and when it goes through them, i want it to call a provided method(object) on each.

The class itself is a template class, so i cannot write the specific code directly in the class.

The method will need some variables from the class that owns the template class.

I figured i can do it either:
a)Make a base class with a virtual method, derieve class to define the method, pass pointer to the template class so it can call it on each object.

b)Add a (protected?) virtual method to the template class, then derieve a class from it to define that method and add any variables needed. The method will now be correctly called by the base class on each object when needed.


I think the second one will be better, because the first one can be changed at runtime, and i dont need to do that.

I dont want to return a list of the objects to be processed by the owning class, because that adds unnecessary overhead and the amount of items is not constant.

So, should i go with the 2nd method or are there any more alternatives?

o3o

Advertisement
You're somewhat vague, but it sounds like you can use a function pointer or a function object (like std::function).

You're somewhat vague, but it sounds like you can use a function pointer or a function object (like std::function).


I cannot/do not want to use a func pointer because:
-Im afraid of them
-I need to access some external state

What im trying to do is a grid scrolling class. It has a x*y grid, and an imaginary x*y rectangle positioned somewhere over that grid (warping around). I can make it scroll rows or columns.

When i scroll, as the cells will warp around, i need to reset the ones that exit the imaginary rectangle (and enter the rectangle from the other side at the same time) and also load any new data to them (think minecraft, the chunks you go away from need to be unloaded, and then i reuse the same cells to load the new chunks that enter the range on the opposite side)

So, i need to have the scroller execute "method(loadAndResetCell)", which needs to access external state to know what data to load in it and its world position and so on (scroller will only provide the relative position on the current "rectangle". If it is in the center of the scrollers area, and its 3*3, position will be 2,2) so actually its method(loadAndResetCell,x,y)

The method is called when the scroller after scrolling figures out and iterates over the cells which have wrapped over to the other side of the rectangle.
a) The method is a virtual method i define in a derieved class
this->method(cell,x,y)
b) I derieve from a base class with a method to define it, then pass a base class pointer to the scroller, so it can call the virtual method
algorithmContainerObjectThingThatWasPassedToMePolymorphismEtc->method(cell,x,y)

o3o

If you need to attach state, then you can still use a function object like std::function possibly with std::bind and lambdas, or the boost versions of function and bind if you don't have an up to date compiler.
You mean like passing a function to the scroller?

How would i pass the state the function needs to access, the only way i can imagine is to make the state type a template param so the scroller can contain it, with a function to set it, and make the scroller then call this->method(this->stateForMethod)

Is it ok if i go with

class Scroller
{

protected:
virtual method(T&,x,y){T=T()}
}

class MyScroller: public Scroller
{
protected:
//ignore ugly code
MapView& view;
virtual method(T&,x,y){T=T() T.LoadFromPosition(view.ScrollPosToWorld(x,y))}
}

Which lets me use it like

*define MyScroller*
class mapview
{
MyScroller scroller(this);
pos ScrollPosToWorld(x,y);
}

o3o


How would i pass the state the function needs to access


If you need to attach state, then you can still use a function object like std::function possibly with std::bind and lambdas, or the boost versions of function and bind if you don't have an up to date compiler.

First google result for "std function"
First google result for "std bind"
First google result for "C++ lambda"

[quote name='Waterlimon' timestamp='1353517230' post='5002949']
How would i pass the state the function needs to access


If you need to attach state, then you can still use a function object like std::function possibly with std::bind and lambdas, or the boost versions of function and bind if you don't have an up to date compiler.

First google result for "std function"
First google result for "std bind"
First google result for "C++ lambda"
[/quote]
Yeah i could use lambdas for this, but do you suggest me to use them over derieving a class from the scroller class and having the method be virtual that the class itself uses?
Because im unsure about their support and whether the code will be as easy to understand for me...

EDIT: So now i need to choose between
-Functor
-Lambda
-Derieving from scroller

i think i will go with functor using a class with () overloaded and maybe try to use it with lambdas too (should work right, if i pass the functor as template variable?)

o3o

This topic is closed to new replies.

Advertisement