" in C ++ Possible duplicate: When do I use a period, arrow, or double colon to refer to class memb...">

What's the difference between "::" "." and "->" in C ++

Possible duplicate:
When do I use a period, arrow, or double colon to refer to class members in C ++?

I created a class called Kwadrat, and I have three internal fields inside. The code block gives me advice that I can get into the field of the :: object . and -> . An arrow is only a job, but why? What is the difference between the three?

 #include <iostream> using namespace std; class Kwadrat{ public: int val1, val2, val3; Kwadrat(int val1, int val2, int val3) { this->val1 = val1; //this.val2 = val2; //this::val3 = val3; } }; int main() { Kwadrat* kwadrat = new Kwadrat(1,2,3); cout<<kwadrat->val1<<endl; cout<<kwadrat->val2<<endl; cout<<kwadrat->val3<<endl; return 0; } 
+63
c ++ reference class
Aug 10 2018-12-12T00:
source share
8 answers

1. -> to access object member variables and methods using pointer to an object

 Foo *foo = new Foo(); foo->member_var = 10; foo->member_func(); 

2 . to access object member variables and methods through an instance object

 Foo foo; foo.member_var = 10; foo.member_func(); 

3. :: to access static variables and methods of a class/struct or namespace . It can also be used to access variables and functions from another area (in fact, class, structure, namespace are areas in this case)

 int some_val = Foo::static_var; Foo::static_method(); int max_int = std::numeric_limits<int>::max(); 
+132
Aug 10 2018-12-12T00:
source share

In C ++, you can access fields or methods using different operators, depending on the type:

  • ClassName :: FieldName : public static field and class methods
  • ClassInstance.FieldName : access to a public field (or method) through a class reference
  • ClassPointer-> FieldName : access to a public field (or method) dereferencing a class pointer

Note that :: should be used with the class name, not with the class instance, since static fields or methods are common to all class instances.

 class AClass{ public: static int static_field; int instance_field; static void static_method(); void method(); }; 

then you will get access to this path:

 AClass instance; AClass *pointer = new AClass(); instance.instance_field; //access instance_field through a reference to AClass instance.method(); pointer->instance_field; //access instance_field through a pointer to AClass pointer->method(); AClass::static_field; AClass::static_method(); 
+20
Aug 10 2018-12-12T00:
source share

Putting a very simple :: is the scope operator,. is the access operator (I forgot what the actual name is?), and -> is the dereference arrow.

:: - The scope of the function. That is, it allows the compiler to find out in which class the function works, and therefore how to call it. If you use this operator to call a function, the function is a static function.

. - This allows you to access a member function on an already created object. For example, Foo x; x.bar() Foo x; x.bar() calls the bar() method on an instance of x that is of type Foo . You can also use this to access public class variables.

-> - Essentially the same as . , except that it works with pointer types. Essentially, it plays a pointer, not a call . . Using this is equivalent to (*ptr).method()

+12
Aug 10 2018-12-12T00:
source share

You have a pointer to an object. Therefore, you need to access the field of the object that the pointer points to. To track down the pointer you use * , and to access the field, you use . so you can use:

 cout << (*kwadrat).val1; 

Note that brackets are required. This operation is quite common that for a long time (when C was young) they decided to create a “shorthand” way to do this:

 cout << kwadrat->val1; 

They are identified as identical. As you can see, -> basically just combines * and . in one operation. If you were dealing directly with an object or an object reference, you could use . without dereferencing the pointer first:

 Kwadrat kwadrat2(2,3,4); cout << kwadrat2.val1; 

:: is the region resolution operator It is used when you only need to qualify a name, but you do not deal with a single object at all. This would be primarily for accessing a static data item:

 struct something { static int x; // this only declares `something::x`. Often found in a header }; int something::x; // this defines `something::x`. Usually in .cpp/.cc/.C file. 

In this case, since x is static , it is not associated with any particular instance of something . In fact, it will exist even if no instance of this type of object has been created. In this case, we can access it using the scope resolution operator:

 something::x = 10; std::cout << something::x; 

Note, however, that it also allows access to the static member as if it were a member of a specific object:

 something s; sx = 1; 

At least if the memory serves, at the beginning of the C ++ history this was not allowed, but the meaning is unambiguous, so they decided to allow it.

+7
Aug 10 2018-12-12T00:
source share

The three statements have related but different meanings, despite the misleading note from the IDE.

The :: operator is known as the scope resolution operator and is used to move from a namespace or class to one of its members.

The operators . and -> are intended to access the members of the object instance and enter the game only after creating the object instance. You are using . if you have an actual object (or a reference to an object declared using & in the declared type), and you use -> if you have a pointer to an object (declared using * in the declared type).

This object is always a pointer to the current instance, so why the -> operator is the only one that works.

Examples:

 // In a header file namespace Namespace { class Class { private: int x; public: Class() : x(4) {} void incrementX(); }; } // In an implementation file namespace Namespace { void Class::incrementX() { // Using scope resolution to get to the class member when we aren't using an instance ++(this->x); // this is a pointer, so using ->. Equivalent to ++((*this).x) } } // In a separate file lies your main method int main() { Namespace::Class myInstance; // instantiates an instance. Note the scope resolution Namespace::Class *myPointer = new Namespace::Class; myInstance.incrementX(); // Calling a function on an object instance. myPointer->incrementX(); // Calling a function on an object pointer. (*myPointer).incrementX(); // Calling a function on an object pointer by dereferencing first return 0; } 
+6
Aug 10 2018-12-12T00:
source share

"::" is for static members.

+1
Aug 10 2018-12-12T00:
source share

-> designed for pointers to an instance of a class

. for class instances

:: for classes - for example, when using a static member

+1
Aug 10 2018-12-12T00:
source share

Others answered different syntaxes, but couts that when you do your couts , you only use -> :

 int main() { Kwadrat* kwadrat = new Kwadrat(1,2,3); cout<<kwadrat->val1<<endl; cout<<kwadrat->val2<<endl; cout<<kwadrat->val3<<endl; return 0; } 
0
Aug 10 2018-12-12T00:
source share



All Articles