1

I want to pass class pointer and a functor as a class template argument. I also dont want that that class pointer to be a local member of functor. I want to use an existing template class which is used elsewhere in the code, so I want to apply a default value to these two parameters so I dont have to change the existing code.

My intention is explained in this class.

     class MyClass
        {

        int local;
        /*Dont have member pointer in my current template*/
        AnotherClass* anClass; 

        MyClass(AnotherClass* ptr)
        {
    /*dont want to create a brand new pointer in my template
the local pointer must be assigned a reference to a pre-existing pointer
passed as argument*/

        anClass = ptr;        
        }

        void DoSomething(int val )
        {

        local = InvokeLogic(val);

        /*Dont have this function call DoSomeMore() in my template.
        So I want another DoSomeMore() template argument which 
        is a function-object/functor */

        anClass->DoSomeMore(local); 

        }

        }

However, in reality, I have this template thats currently missing some of the information that I want.

 template <typename T>
    class MyClass
    {

    T local; 

    /* AnotherClass* anClass; does not exist */

    void DoSomething(T t)
    {
    local = InvokeLogic(t);
    /* want to call this anClass->DoSomeMore(local); 

    but dont know how to call it. The DoSomething() function object can accept AnotherClass* anClass as a function argument.
    */

    }

    };

AnotherClass* anClass exists when the instance of MyClass is created.

bsobaid
  • 955
  • 1
  • 16
  • 36
  • Can you pass `&AnotherClass::DoSomething` as an argument (template or function)? – Kerrek SB Jan 29 '16 at 21:02
  • You can't use a value that's computed at runtime as a template parameter. – molbdnilo Jan 29 '16 at 21:04
  • thats correct @molbdnilo. so how do I do what I want to do in the template? – bsobaid Jan 29 '16 at 21:07
  • Doesn't `DoSomething(T t, AnotherClass*, void (AnotherClass::*func)(T))` fit the bill? – SergeyA Jan 29 '16 at 21:14
  • thnx Sergey, so you are passing a function pointer as an argument. can you show how to actually make this call anClass->DoSomeMore(local); in the DoSomething()? is there a way to do the same with a functor instead of function pointer? – bsobaid Jan 29 '16 at 21:21

0 Answers0