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:
C++ - Difference between (*). and ->?
What is the difference between this:
(*ptr).f();
and this:
ptr->f();
in c++ where the ptr is a pointer to C++ class which has a function f?
If ptr is a normal pointer, then both are equivalent. ptr->f is a short-cut to dereference the pointer (equivalent to (*ptr)) and access the member of the dereferenced object (equivalent to .f).
If ptr is a class that overloads operator-> and operator*, then they will each call different operator overloads, and so could have different behaviour.
There's no difference at all. (*ptr).f(); is the uglier way to do this.
Actually, if ptr is some smart pointer and its operator* and operator-> are overloaded and execute some side-effects, then you may have a problem with this. But this is really, really bad thing to do. It's as evil as #define true false
Aside from stylistic/typing differences, there is no difference. It's exactly the same as (*ptr).member = 7; vs ptr->member = 7; when using a pointer to a structure or class.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does (char *) x or (void *) z mean?
I am working with a c++ file and have encountered the following line:
tmp.sort(Hash::pairval, printPair, (void *)(tmp.bitSize()));
I am most unsure of what (void *) means. bitsize() is a function, and I have heard the term passing a function pointer before. Is that what this is?
I know the :: is normally the scope resolution operator, which I have seen in .cpp/.h object type files. I believe it is serving the same purpose here, to state that pairval is found in Hash.
Thanks
The (void *) is simply casting the return value of tmp.bitSize() to a void pointer type. Casting is a very common operation in C++ and c as well.
Hash::pair
Is most probably a call to a static member of class Hash.
The (void*) part is a cast to void pointer of tmp.bitSize() which most probably returns some kind of value. So there is no function pointer.
I am most unsure of what (void *) means. bitsize() is a function, and I have heard the term passing a function pointer before. Is that what this is?
Nope. Note the parentheses, tmp.bitSize() is a function call expression that is called and returns a value. Hence - no function pointers involved here.
The return value is then cast to the pointer-to-void type (i.e. the catch-all "pointer to something" type) in order to be passed to a function which expects such pointer.
Why on Earth would someone convert a bit size (which looks like a number) into a pointer, I have no idea. This is somewhere between dubious and incorrect.
Read up on casting in C++. C-style casts are discouraged and casting to void* is seldomly useful and often dangerous because of the strict aliasing rule.
know the :: is normally the scope resolution operator, which I have seen in .cpp/.h object type files. I believe it is serving the same purpose here, to state that pairval is found in Hash.
That's correct.
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->
This question already has answers here:
What can I use instead of the arrow operator, `->`?
(7 answers)
Closed 5 years ago.
What is the difference between the dot (.) operator and -> in C++?
foo->bar() is the same as (*foo).bar().
The parenthesizes above are necessary because of the binding strength of the * and . operators.
*foo.bar() wouldn't work because Dot (.) operator is evaluated first (see operator precedence)
The Dot (.) operator can't be overloaded, arrow (->) operator can be overloaded.
The Dot (.) operator can't be applied to pointers.
Also see: What is the arrow operator (->) synonym for in C++?
For a pointer, we could just use
*pointervariable.foo
But the . operator has greater precedence than the * operator, so . is evaluated first. So we need to force this with parenthesis:
(*pointervariable).foo
But typing the ()'s all the time is hard, so they developed -> as a shortcut to say the same thing. If you are accessing a property of an object or object reference, use . If you are accessing a property of an object through a pointer, use ->
Dot operator can't be overloaded, arrow operator can be overloaded. Arrow operator is generally meant to be applied to pointers (or objects that behave like pointers, like smart pointers). Dot operator can't be applied to pointers.
EDIT
When applied to pointer arrow operator is equivalent to applying dot operator to pointee e.g. ptr->field is equivalent to (*ptr).field.
The arrow operator is like dot, except it dereferences a pointer first. foo.bar() calls method bar() on object foo, foo->bar calls method bar on the object pointed to by pointer foo.
The . operator is for direct member access.
object.Field
The arrow dereferences a pointer so you can access the object/memory it is pointing to
pClass->Field
pSomething->someMember
is equivalent to
(*pSomething).someMember
Use -> when you have a pointer.
Use . when you have structure (class).
When you want to point attribute that belongs to structure use .:
structure.attribute
When you want to point to an attribute that has reference to memory by pointer use -> :
pointer->method;
or same as:
(*pointer).method
The target.
dot works on objects; arrow works on pointers to objects.
std::string str("foo");
std::string * pstr = new std::string("foo");
str.size ();
pstr->size ();
Note that the -> operator cannot be used for certain things, for instance, accessing operator[].
#include <vector>
int main()
{
std::vector<int> iVec;
iVec.push_back(42);
std::vector<int>* iVecPtr = &iVec;
//int i = iVecPtr->[0]; // Does not compile
int i = (*iVecPtr)[0]; // Compiles.
}
It's simple, whenever you see
x->y
know it is the same as
(*x).y
The -> is simply syntactic sugar for a pointer dereference,
As others have said:
pointer->method();
is a simple method of saying:
(*pointer).method();
For more pointer fun, check out Binky, and his magic wand of dereferencing:
http://www.youtube.com/watch?v=UvoHwFvAvQE
The simplest difference between the two is that "->" dereferences a pointer before it goes to look at that objects fields, function etc. whereas "." doesn't dereference first. Use "->" when you have a pointer to an object, and use "." when you're working with the actual instance of an object.
Another equivalent way of wrinting this might be to use the dereferencing "*" on the pointer first and then just use the ".". We skip middleman by using "->".
There are other differences, but the other answers have covered this extensively.
If you have a background in Java this might confuse you, since, in Java, everything is pointers. This means that there's no reason to have symbol that doesn't dereference your pointer first. In c++ however you gotta be a little more careful with remembering what is and what isn't a pointer, and it might be a good idea to label them with the prefix "p_" or simply "p".
The . (dot) operator is usually used to get a field / call a method from an instance of class (or a static field / method of a class).
p.myField, p.myMethod() - p instance of a class
The -> (arrow) operator is used to get a field / call a method from the content pointed by the class.
p->myField, p->myMethod() - p points to a class
The -> operator is used when we are working with a pointer and the dot is used otherwise.
So if we have a struct class like:
struct class{ int num_students; int yr_grad; };
and we have an instance of a class* curr_class (class pointer), then to get access to number of students we would do
cout << curr_class->num_students << endl;
In case we had a simple class object , say class_2016, we would do
cout << class_2016.num_students << endl;
For the pointer to class the -> operator is equivalent to
(*obj).mem_var
Note: For a class, the way to access member functions of the class will also be the same way