That is .*
- access to a pointer to a member of a class and ->*
- access to a pointer to a member of a class by a pointer. Show with examples.
3 answers
This is not a complete list of examples:
Yes, all this is in English, but there are examples on C ++, and he is also known in Africa C ++.
- oneOnce again I am convinced that studying C ++ is a matter of a lifetime))) - skegg
- Yes, live and learn. But without practice, I still forget in a month that it can be called that way. - avp
- [KoVadim] [1], Thanks for the links. In the latter I found [this] [2] post, perhaps I will associate with it the expediency of applying, until I meet something else in practice. [1]: hashcode.ru/users/2739/kovadim [2]: stackoverflow.com/questions/670734/… - mzarb
A simple example:
class My{ void(My::*z)( int ); // void state_1( int x ){ if( x == 1 ){ z = &My::state_2; } } void state_2( int x ){ if( x == 2 ){ z = &My::state_3; }else{ z = &My::state_1; } } void state_3( int x ){ if( x == 3 ){ z = &My::state_1; } } // public: My( void ) : z( &My::state_1 ){} void state( int x ){ (this->*z)( x ); } };
Here, in the class My
there is a field z
, which stores the current state of an object of class My
. Depending on the external influence x
, this condition may vary.
This is how an ordinary state machine can become more understandable with "pointers to class members."
Operator (.) Point applies to ordinary objects, for example:
class C{ pubic: int value; int method(); }; int main()... C c; // объект c.value = 3; // обращение к данным c.method(); // вызов метода
the same applies to structures:
struct C{ pubic: int value; int method(); }; int main()... C c; // обьект c.value = 3; // обращение к данным c.method(); // вызов метода
to objects in memory, use the operator (->) arrow:
class C{ pubic: int value; int method(); }; int main()... C * p; // указатель на обьект p = new C; // создаем объект в памяти c->value = 3; // обращение к данным c->method(); // вызов метода
the same applies to structures:
struct C{ pubic: int value; int method(); }; int main()... C * p; // указатель на обьект p = new C; // создаем объект в памяти c->value = 3; // обращение к данным c->method(); // вызов метода
the (->) arrow operator is also applied inside the class to the (this) pointer, for example:
class C{ pubic: int value; int method(){ this->value = 10; // указатель на себя } };
- this-> value = 10; And why is this necessary? In fact, when compiling, there will be no classes left, but there will be functions with a hidden parameter * this and it will itself be substituted for variables, for example, just write value and it will work, and then the compiler will substitute this-> value, that is, there is no difference is indicated explicitly, the compiler will implicitly substitute. Did I understand correctly? Or there are cases when it is necessary to specify this? And that I saw only as through * this object is returned. - mzarb
- for example, we overload the operator (=) equals. To make sure that the object does not copy itself, you can check the address of the objects on the right and left of the operator (=) equal to - perfect
- one@perfect, did you read the question carefully? - mega
- no wrong. read only the title. I apologize - perfect
- Well, in general, then I will answer so that the operator (->) replaces (. *) and everything else - perfect