Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling member functions from within another member function of the same class in C++, objective C

Consider the following:

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

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?

like image 258
Namratha Avatar asked Feb 08 '11 06:02

Namratha


1 Answers

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.

like image 107
Ens Avatar answered Sep 20 '22 15:09

Ens



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!