c++ - Use or don't use 'this' within a object -
this question has answer here:
my question refers case when want call other methods of same class. where's difference of doing , without use of 'this'? same variables of class. there difference accessing variables through 'this'? related whether methods / variables private / public? example:
class { private: int i; void print_i () { cout << << endl; } public: void do_something () { this->print_i(); //call 'this', or ... print_i(); //... call without 'this' this->i = 5; //same setting member; = 5; } };
generally, it's question of style. of places i've worked have preferred not using this->
, except when necessary.
there cases makes difference:
int func(); template <typename base> class derived : public base { int f1() const { return func(); // calls global func, above. } int f2() const { return this->func(); // calls member function of base } };
in case, this->
makes name of function dependent, in turn defers binding when template instantiated. without this->
, function name bound when template defined, without considering might in base
(since isn't known when template defined).
Comments
Post a Comment