This question already has answers here:
What is the difference between the dot (.) operator and -> in C++? [duplicate]
(14 answers)
Closed 9 years ago.
I would like to create a linked list.
For the following code:
struct sampleStruct
{
int a;
sampleStruct *next = NULL;
};
sampleStruct *sample = new sampleStruct;
What is the difference between sample.next and sample->next?
Okay to explain it in a more complete way. The most other guys had already wrote that you have to use '->' when ever you have a pointer. But you could also do this with '.', to do this you must respect the priority of the operators. We need the '*' to get the content of the pointer, but this has a lower priority than the '.', so you must write it into brackets to give it a higher priority, so when you want to do this with a '.' you have to wrote:
(*sample).next
and you see this is a complex syntax, to do this in a more easy way the '->' was introduced. With it you could write code in a more comfortable way.
So this is is equal to the example and it looks much better.
sample->next
Since sample is a pointer, there can't be any way to access a data member through . rather than the indirection operator ->. For example, this won't compile:
sample.next; // error: member reference type 'sampleStruct *' is a pointer;
// maybe you meant to use '->'?
The error actually speaks for itself.
Related
This question already has an answer here:
About meaning of ::* on this code [duplicate]
(1 answer)
Closed 5 years ago.
While refactoring a piece of code I came across the line below:
class Bar
{
protected:
int (Bar::* fooFunction)(float); //this line
}
I have never seen this kind of syntax before. What is this syntax and why is it used it used in C++?
It's a member function pointer.
Specifically, it's a pointer to a member function of a Bar object that takes a float argument and returns a int.
Read more here: http://en.cppreference.com/w/cpp/language/pointer#Pointers_to_member_functions
What is this syntax
It declares a pointer to member function.
why is it used it used in C++?
It is used to point to non-static member functions.
This question already has answers here:
How does dereferencing of a function pointer happen?
(5 answers)
Closed 6 years ago.
Suppose I have the following types:
typedef uint8 (*P2MyFunc)(void);
typedef struct
{
P2MyFunc ptr;
}MyStruct;
Given an instance inst of MyStruct. is there any difference at all between the following calls ?
(*inst.ptr)();
inst.ptr();
Both seem to work just fine but the first one might be prone to compiler warnings.
They mean the exact same thing according to the C Standard. You could even go a step further. The following below give the same result:
(********inst.ptr)();
inst.ptr();
This question already has answers here:
What are the pointer-to-member operators ->* and .* in C++?
(7 answers)
Closed 7 years ago.
I came across code today that looked like:
result = (this->*(*c))(¶m)
The main part that confuses me is the this->*(*c) What does it mean to have the asterisk operators between the arrow (->) and the name of the variable we're accessing (c).
What you have here is an operator which you don't see very often.
->* is a single operator. It is the pointer-based counterpart to .* and is a member access operator.
It is used if you have an object to use a member on (e.g. a function), but don't know the concrete member (it's stored in a variable).
Let's split it up:
this // object to work on
->* // member access operator
(*c) // dereference pointer pointing to member function (c is a pointer-to-pointer)
(¶m) // call member function stored in c on this passing ¶m to the function
See also: http://en.cppreference.com/w/cpp/language/operator_member_access
Edit: This post also contains a good explaination on what is happening here: https://stackoverflow.com/a/6586248/1314789
The parse tree for the expression is this:
=
/ \
result function call
/ \
->* &
/ \ |
this * param
|
c
The parentheses are necessary because of boring grammar reasons.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
ptr->hello(); /* VERSUS */ (*ptr).hello();
I am learning C++ and my question is if there is any difference between using the arrow operator (->) or dereferencing the pointer * for calling a function.
These two cases illustrate my question.
Class* pointer = new Class();
(*pointer).Function(); // case one
pointer->Function(); // case two
What is the difference?
If the operators * and -> aren't overloaded, both versions accomplish the same.
Given
Class* pointer = new Class();
Then
(*pointer).Function(); // case one
dereferences the pointer, and calls the member function Function on the referred to object. It does not use any overloaded operator. Operators can't be overloaded on raw pointer or built-in type arguments.
pointer->Function(); // case two
This does the same as the first one, using the built-in -> because pointer is a raw pointer, but this syntax is better suited for longer chains of dereferencing.
Consider e.g.
(*(*(*p).pSomething).pSomethingElse).foo()
versus
p->pSomething->pSomethingElse->foo()
The -> notation is also more obvious at a glance.
This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
Why does C have a distinction between -> and . ?
Lets say that I have this structure:
struct movies
{
string title;
int year;
} my_movie, *ptrMovie;
Now I access my_movie like this: my_movie.year = 1999; Now to access a pointer I must do this: ptrMovie->year = 1999;
Why do pointers use the -> operator and normal data types use the . operator? Is there any reason they couldn't both use the . operator?
The . operator accesses a member of a structure and can operate only on structure variables.
If you want to do this to a pointer, you first need to dereference the pointer (using *) and then access the member (using .). Something like
(*ptrMovie).year = 1999
The -> operator is a shorthand for this.
The . operator is only valid for a struct or class. A pointer is not a struct or class, so you need to dereference your pointer to get the struct/class it is pointing to like this
(*ptrMovie).year
The member operator . has a higher precedence than the dereference operator *, so you need to enclose the dereferencing operation in parenthesis. Or you could do this
ptrMovie->year
Both are equivalent. The '->' operator is a shortcut for dereferencing your pointer and then accessing a struct member. It is less typing and a little nicer to use in my opinion. Apparently most people agree with me because that is the standard way to access struct members from a pointer to the struct in most code that I've seen. You especially appreciate the difference when you have to do multiple levels of indirection:
ptrToStruct->memberPtr->subMemberPtr->subsubPtr->subsubsubPtr->x
(*(*(*(*(ptrToStruct).memberPtr).subMemberPtr).subsubPtr).subsubsubPtr).x
Both of those statements are equivalent, but the first is easier to work with.
If they both used . how could you differentiate between the pointer and the actual object?
To me:
->
Reminds me of an arrow which points to something, so I find it great that -> is used.
Instead of typing (*myPointer). it is simplier to use myPointer->