简体   繁体   English

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

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

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. funcTempB在其实现中调用funcTempA 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?
}

To call a member variable or a member function, you can use this keyword. 要调用成员变量或成员函数,可以使用this关键字。

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 你可以阅读这个知道矿石约this

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 生活

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM