Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between this->field and Class::field?

I'm wondering something in C++.

Admitting the following code:

int bar;
class Foo
{
public:
    Foo();
private:
    int bar;
};

Inside my class, is there any difference between this->bar and Foo::bar? Are there cases where one is invalid?

like image 361
Bryan Peeters Avatar asked Apr 27 '13 22:04

Bryan Peeters


People also ask

What is the difference between :: and in C++?

Put very simple :: is the scoping operator, . is the access operator (I forget what the actual name is?), and -> is the dereference arrow.

What's the difference between a class field property?

Fields should (almost always) be kept private to a class and accessed via get and set properties. Properties provide a level of abstraction allowing you to change the fields while not affecting the external way they are accessed by the things that use your class.

What is a class field in Java?

A field is a class, interface, or enum with an associated value. Methods in the java. lang. reflect. Field class can retrieve information about the field, such as its name, type, modifiers, and annotations.

What is the meaning of class field?

Given a set of primes, a field is called a class field if it is a maximal normal extension of the rationals which splits all of the primes in , and if is the maximal set of primes split by K. Here the set. is defined up to the equivalence relation of allowing a finite number of exceptions.

Why would you use class field in C#?

A field, in C#, is a member of a class or an object of any type that represents a memory location for storing a value. Fields are used to store data that must be accessible to multiple methods of a class and available throughout the lifetime of an object.

What is the difference between field and property in Java?

A Java property is also much like a field. The real difference is in their intended scope. Fields are meant to be private or protected in scope, meaning that access is restricted. Properties are meant to be public in scope, meaning that access is not restricted.


2 Answers

Inside class Foo (specifically) there is no difference between the two given that bar is not static.

Foo::bar is called the fully qualified name of the member bar, and this form is useful in scenarios where there may be several types in the hierarchy defining a member with the same name. For example, you would need to write Foo::bar here:

class Foo
{
  public: Foo();
  protected: int bar;
};

class Baz : public Foo
{
  public: Baz();
  protected: int bar;

  void Test()
  {
      this->bar = 0; // Baz::bar
      Foo::bar = 0; // the only way to refer to Foo::bar
  }
};
like image 190
Jon Avatar answered Oct 04 '22 19:10

Jon


They do the same thing members.

However, you wont be able to use this-> to distinguish members of the same name in the class hierarchy. You will need to use the ClassName:: version to do that.

like image 26
pmr Avatar answered Oct 04 '22 19:10

pmr