简体   繁体   中英

C++: when we should use “this->” PLUS parameters by reference

suppose we have a class

class Foo {
private:
        int PARTS;
public:
        Foo( Graph & );
        int howBig();
}

int Foo::howBig() { return this->PARTS; }
int Foo::howBig() { return PARTS;       }
Foo::Foo( Graph &G ) {
    <Do something with G.*>
}

Which one of howBig()-variants is correct? The &-sign ensures that only the reference for Graph object is passed to initialization function? In CI would simply do something like some_function( Graph *G ), but in C++ we have both & and *-type variables, never understood the difference...

Thank you.

When you've local variable inside a member function, then you must have to use this as:

 Foo::MemberFunction(int a)
 {
      int b = a;  //b is initialized with the parameter (which is a local variable)
      int c = this->a; //c is initialized with member data a
      this->a = a; //update the member data with the parameter
 }

But when you don't have such cases, then this is implicit; you need to explicity write it, which means in your code, both versions of howBig is correct.

However, in member initialization list, the rules are different. For example:

struct A
{
   int a;
   A(int a) : a(a) {}
};

In this code, a(a) means, the member data a is being initialized with the parameter a . You don't have to write this->a(a) . Just a(a) is enough. Understand this visually:

   A(int a) : a ( a ) {}
   //         ^   ^
   //         |   this is the parameter
   //         this is the member data

You can use this-> to resolve the dependent name issue without explicitly having to spell out the name of the base. If the name of the base is big this could arguably improve readability.

This issue only occurs when writing templates and using this-> is only appropriate if they're member functions, eg:

template <typename T>
struct bar {
    void func();
};

template <typename T>
struct foo : public bar {
    void derived()
    {
        func(); // error
        this->func(); // fine
        bar<T>::func(); // also fine, but potentially a lot more verbose
    }
};

Which one of howBig()-variants is correct?

both in your case, the compiler will produce the same code

The &-sign ensures that only the reference for Graph object is passed to initialization function? In CI would simply do something like some_function( Graph *G ), but in C++ we have both & and *-type variables, never understood the difference...

there is no difference as per the use of the variable inside the method(except syntax) - in the case of reference(&) imagine as if you've been passed an invisible pointer that you can use without dereferencing it(the &) might be "easier" for clients to use

  1. Both forms of Foo::howBig() are correct. I tend to use the second in general, but there are situations that involve templates where the first is required.
  2. The main difference between references and pointers is the lack of "null references". You can use reference arguments when you don't want to copy the whole object but you want to force the caller to pass one.
  1. Both are correct. Usually shorter code is easier to read, so only use this-> if you need it to disambiguate (see the other answers) or if you would otherwise have trouble understanding where the symbol comes from.

  2. References can't be rebound and can't be (easily) bound to NULL, so:

    1. Prefer references to pointers where you can use them. Since they cannot be null and they cannot be deleted, you have fewer things to worry about when using code that uses references.
    2. Use const references instead of values to pass objects that are large (more than say 16 or 20 bytes) or have complex copy constructors to save copy overhead while treating it as if it was pass by value.
    3. Try to avoid return arguments altogether, whether by pointer or reference. Return complex object or std::pair or boost::tuple or std::tuple (C++11 or TR1 only) instead. It's more readable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM