D
D
Denis2021-08-26 15:05:16
C++ / C#
Denis, 2021-08-26 15:05:16

What is the difference between the -> operator and .(dot) operator?

I have a simple question, why is the -> operator used when referring to a dynamic class object, and not . ? Yes, the IDE (In my case, Visual Studio 2019) itself replaces the "dot" with an "arrow", but why is this so? What's the Difference?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Armenian Radio, 2021-08-26
@NeYmen

a -> b is a shorter notation for (* a ). b
That is, an arrow combines a pointer dereference and a method call.
Well, if a is a class, then the arrow operator can be defined in it at the request of the class author.

G
GavriKos, 2021-08-26
@GavriKos

The compiler does not replace anything - apparently this is what your IDE does.
The difference is in the purpose. You will find in any textbook on pluses.

R
Rsa97, 2021-08-26
@Rsa97

EMNIP
a->b(*a).b

R
res2001, 2021-08-26
@res2001

The arrow is used with pointers to a class/struct, while the dot is used with references or directly with a class/struct instance. Those. if you try to use a dot with a pointer, you will get a syntax error.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question