Search code examples
c++class-template

Calling another member function from a function in a class template in C++


Let's say I have a class template named myTemplate with some member variables and two member functions, funcTempA, and funcTempB.

template <class T>
class myTemplate
{
    private:
        //member variables
    public:
        T* funcTempA(T *arg1, T *arg2);
        T* funcTempB(T *arg1, T *arg2);
}

funcTempB calls funcTempA in its implementation. I just want to know what will be the correct syntax for calling it.

template <class T>
T* funcTempB(T *arg1, T *arg2)
{
    //how to call funcTempA here?
}

Solution

  • Just call it directly, such as:

    return funcTempA(arg1, arg2);
    

    BTW: The definition of the member function funcTempB seems wrong, might cause some unexpected errors.

    template <class T>
    T* myTemplate<T>::funcTempB(T *arg1, T *arg2)
    // ~~~~~~~~~~~~~~~
    {
        return funcTempA(arg1, arg2);
    }
    

    LIVE