Should I use `this` within a class?

前端 未结 7 1633
小蘑菇
小蘑菇 2020-12-14 07:20

Within a member function of a class in C++, does it make a difference, if I use this->dataMember or just dataMember? What is considered better

相关标签:
7条回答
  • 2020-12-14 07:26

    If a template function makes a call to a member function such that the call does not depend on any template parameters, this-> can be used to help the compiler as an alternative to MyUtopicClass<int, double, double>::vin().

    0 讨论(0)
  • 2020-12-14 07:28

    using "this->" is better (you are sure it's the members) but it's doesn't make a difference

    0 讨论(0)
  • 2020-12-14 07:33

    I always use this when calling member functions.

    1. It turns the function name into a dependent name so that base class member functions are found within a class template.
    2. It suppresses argument-dependent lookup. ADL has its advantages, but it can lead to surprising behavior, and I like it if it's not getting in my way.
    3. It has no real disadvantages, and so I use it for all member function calls for consistency reasons.
    4. I program in Python a lot where an explicit self is mandatory, so it's not a real burden for me.

    But for data members I use it only when necessary because there is no ADL taking place. To answer your specific questions:

    Within a member function of a class in C++, does it make a difference, if I use this->dataMember or just dataMember?

    Yes, if this is within a class template. Then dataMember is considered a non-dependent name, which can lead to semantic differences. For example:

    #include <iostream>
    
    int i = 1;
    
    struct R {
      int i;
      R(): i(2) { }
    };
    
    template<typename T>
    struct S: T {
      void f() {
        std::cout << i << ' '     // selects ::i
                  << this->i      // selects R::i
                  << std::endl;
      }
    };
    
    int main() {
      S<R>().f();
    }
    

    What is considered better style?

    I don't think that there is a strong opinion within the community about this. Use either style, but be consistent.

    Is there any performance difference?

    I'm pretty sure there isn't.

    0 讨论(0)
  • 2020-12-14 07:33

    use this when you have a hidden/private member =) in any other case it does not make a difference =)

    from the IBM information center i quote the following

    Unless a class member name is hidden, using the class member name is equivalent to using the class member name with the this pointer and the class member access operator (->).

    0 讨论(0)
  • 2020-12-14 07:34

    it's simply redundant to use this-> to call members, unless you want to semantically distinguish between locals and members quickly. a lot of people use the m_ prefix for class members, to avoid writing this-> all the time.

    0 讨论(0)
  • 2020-12-14 07:35

    This is a matter of style. Some people like the extra this-> to make it more obvious that you are accessing a class member. But if you feel it's obvious enough without it, there will be no difference in the generated code or performance.

    (Besides the case you mentioned with overlapping scopes, this-> can also be mandatory in a template when trying to name a member of a type-dependent base class.)

    0 讨论(0)
提交回复
热议问题