从c++中的类模板中的函数调用另一个成员函数

时间:2022-09-06 21:37:21

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

假设我有一个名为myTemplate的类模板,其中包含一些成员变量和两个成员函数funcTempA和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.

funcTempB在其实现中调用funcTempA。我只是想知道调用它的正确语法是什么。

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

2 个解决方案

#1


3  

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.

顺便说一句:成员函数funcTempB的定义似乎是错误的,可能会导致一些意想不到的错误。

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

LIVE

生活

#2


2  

To call a member variable or a member function, you can use this keyword.

要调用成员变量或成员函数,可以使用这个关键字。

template <class T>
T* myTemplate<T>::funcTempB(T *arg1, T *arg2)
{
    this->funcTempA(arg1, arg2);
    return ...;
}

You can read this to know ore about this

你可以读读这个来了解更多

#1


3  

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.

顺便说一句:成员函数funcTempB的定义似乎是错误的,可能会导致一些意想不到的错误。

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

LIVE

生活

#2


2  

To call a member variable or a member function, you can use this keyword.

要调用成员变量或成员函数,可以使用这个关键字。

template <class T>
T* myTemplate<T>::funcTempB(T *arg1, T *arg2)
{
    this->funcTempA(arg1, arg2);
    return ...;
}

You can read this to know ore about this

你可以读读这个来了解更多