从C ++中的同一类的另一个成员函数中调用成员函数,目标C.

问题描述:

请考虑以下内容:

class A{

    //data members

    void foo()
    {
        bar();//is this possible? or should you say this->bar() note that bar is not static
    }
    void bar()
    {

    }
}//end of class A

如何从另一个函数中调用成员函数?静态函数如何影响'this'的使用。
应该在对象上调用函数?

How do you call member functions from within another? And how does static functions affect the use of 'this'. Should functions be called on an object?

Nawaz是正确的:'this'一个例外是如果foo是一个静态函数,因为在静态函数中没有this。在这种情况下,你不能使用bar(),除非bar()也是一个静态函数,你不能使用this-> bar()。

Nawaz is correct: 'this' is implicit. The one exception is if foo were a static function, because in static functions there is no 'this'. In that case, you can't use bar() unless bar() is also a static function, and you can't use this->bar() at all.