Hi,
I am new to OOP and I have ended up in a situation that might force me to abondon some of the flexibility I had in mind in my project. Before I do so, I figure I'd ask here to see if there is an easier fix for me.

I have created a big class that does a lot of things and holds a lot of variables. Let's call it DO_MANY_THINGS class. I also have a smaller class that I can see myself re-using in many other projects as it has some goodies in it. Let's call this class GOODIES. GOODIES has a member function that takes a pointer to a function with arguments as one argument. That works fine as long as the function that is pointed to is external to the class, which it was during my testing of it. However, I never realized that pointing to a member function would cause me this headache. I thought that using a function pointer I could have a class that is not tied to anything. But in my case, the function I want to point to is internal to DO_MANY_THINGS. Most of my functions in that class, all but one, are internal in the sense that they dont take an external input but operates on member parameters only.

My solution to this is to move the part I need from GOODIES into DO_MANY_THINGS to have it do even more things. I would then break the flexibility as I would have to hard code the function it operates on and if I want to re-use this in other projects I would have to mess around in it a little to adapt it to another environment... sigh...

Any hints as to what I should do? I have read about wrappers but I dont fully understand it so it seems easier to forget about flexibility and keep moving...

Thanks in advance for any input in this matter!!!

Hi,

You could go with static class methods; here is an example using both global functions and static methods (they are pretty much the same thing):

typedef void(*ParamFunc)(int i);

void ExternalFunctionThatGetsPassedAsParam(int i)
{
    i++;
}

class Goodies
{
public:
    static void SomeFunction(ParamFunc p)
    {
        p(5);
    }
};

class DoManyThings
{
public:
    static void FunctionThatGetsPassedAsParam(int i)
    {
        i++;
    }
};


int _tmain(int argc, _TCHAR* argv[])
{
    ParamFunc p = ExternalFunctionThatGetsPassedAsParam;

    Goodies::SomeFunction(p);
    Goodies::SomeFunction(DoManyThings::FunctionThatGetsPassedAsParam);

	return 0;
}

For actual member methods you'll need a different approach as the Goodies class needs an actual object on which to call the function pointer ... you need templates for that.

Ok, so here's the template variant:

template<class T>class Goodies
{
public:
    typedef void (T::*ParamFunc)(int);
    void SomeFunction(T object, ParamFunc p)
    {
        (object.*p)(5);
    }
};

class DoManyThings
{
public:
    void DoThis(int i)
    {
        i++;
    }
};



int _tmain(int argc, _TCHAR* argv[])
{
    DoManyThings obj;
    Goodies<DoManyThings> goody;
    goody.SomeFunction(obj, &(DoManyThings::DoThis));

    return 0;
}
commented: Great in-depth answer! +1

Thank you very much for your detailed response!!! In my case I think I need the second, template, approach as the function I need to point to, from Goodies, is inside DoManyThings. I'll give it a shot and see what happens. Thanks again!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.