1

我想将类指针和仿函数作为类模板参数传递。我也不希望该类指针成为仿函数的本地成员。我想使用代码中其他地方使用的现有模板类,所以我想对这两个参数应用默认值,这样我就不必更改现有代码。

我的意图在这堂课中得到了解释。

     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); 

        }

        }

然而,实际上,我有这个模板目前缺少一些我想要的信息。

 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.
    */

    }

    };

当 MyClass 的实例被创建时,AnotherClass* anClass 就存在了。

4

0 回答 0