Вопрос по c++, this – Чрезмерное использование `this` в C ++ [дубликат]

22

This question already has an answer here:

When should I make explicit use of the `this` pointer? 13 answers

Я имею дело с большой базой кода, которая использует следующую конструкцию

class MyClass
{
public:
  void f(int x);
private:
  int x;
};


void MyClass::f(int x)
{
'
'
  this->x = x;
'
'
}

Лично я всегда использовал и поэтому предпочитаю форму

class MyClass
{
public:
  void f(int x);
private:
  int _x;
};


void MyClass::f(int x)
{
'
'
  _x = x;
'
'
}

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded Chris Huang-Leaver
Error: User Rate Limit Exceeded SmacL
Error: User Rate Limit ExceededthisError: User Rate Limit Exceededy). Groo
Error: User Rate Limit Exceeded Newtopian
Error: User Rate Limit ExceededsingleError: User Rate Limit ExceededthisError: User Rate Limit Exceeded Groo

Ваш Ответ

15   ответов
1

class MyClass{
public:  
  int x;  
  void f(int xval);
};
//
void MyClass::f(int xval){  
  x = xval;
}
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
3

3

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded SmacL
2

27

Avoid leading underscore: _x is ok until somebody chooses _MyField which is a reserved name. An initial underscore followed by a capital letter is not allowed as a variable name. See: What are the rules about using an underscore in a C++ identifier? Make the attribute private or protected: the change is safe if it compiles, and you'll ensure your setter will be used. The this-> story has a use, for example in templated code to make the field name dependent on your type (can solve some lookup issues).

#include <iostream>
#include <ostream>

class A
{
public:
  int g_;
  A() : g_(1) {}
  const char* f() { return __FUNCTION__; }
};

const char* f() { return __FUNCTION__; }
int g_ = -1;

template < typename Base >
struct Derived : public Base
{
  void print_conflicts()
  {
    std::cout << f() << std::endl; // Calls ::f()
    std::cout << this->f() << std::endl; // Calls A::f()
    std::cout << g_ << std::endl; // Prints global g_
    std::cout << this->g_ << std::endl; // Prints A::g_
  }
};

int main(int argc, char* argv[])
{
   Derived< A >().print_conflicts();
   return EXIT_SUCCESS;
}
Error: User Rate Limit Exceeded SmacL
Error: User Rate Limit Exceeded SmacL
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
0

thism_

0

0

this?

10
Error: User Rate Limit Exceeded SmacL
0

s more normal in C++ for members to be initialised on construction using initialiser.

Error: User Rate Limit Exceeded

Error: User Rate Limit ExceededFoo(int x) { this.x = x; }Error: User Rate Limit Exceeded

Error: User Rate Limit Exceededthis -> xError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

1

m_

3

Error: User Rate Limit ExceededonlyError: User Rate Limit Exceeded
Error: User Rate Limit Exceeded SmacL
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded SmacL
0
class MyClass
{
public:
  int m_x;
  void f(int p_x);
};


void MyClass::f(int p_x)
{
  m_x = p_x;
}

Error: User Rate Limit Exceeded
-2

class tea
{
public:
    int cup;
    int spoon;
    tea(int cups, int spoons);
};

class tea
{
public:
    int cup;
    int spoon;
    tea(int drink, int sugar);
};

Error: User Rate Limit Exceeded
-1

typedef struct _myclass
{
   int _x;
} MyClass;

void f(MyClass this, int x)
{
   this->_x = x;
}

Похожие вопросы