本文介绍了从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.
这篇关于从C ++中的同一类的另一个成员函数中调用成员函数,目标C.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!