I come across this instruction
a=&*A;
in a piece of C++ code, where A is a pointer. Can anyone explain its semantics and in what way it is different from a=A
?
I come across this instruction
a=&*A;
in a piece of C++ code, where A is a pointer. Can anyone explain its semantics and in what way it is different from a=A
?
If A
is an iterator, then *A
gives you the value of what the iterator is "pointing" to. Then the &
operator is applied to that value to get an address to the value. Unless the &
operator was overloaded.
Simple and somewhat stupid example:
struct Foo
{
// Some data...
};
std::vector<Foo> vector_of_foo;
// Code to populate vector_of_foo
for (auto A = vector_of_foo.begin(); A != vector_of_foo.end(); ++A)
{
Foo* a = &*A;
// Do something that requires a pointer to Foo...
}
Operator * can be overloaded, and it usually is overloaded for iterators and smart pointers.
If A
is a class with pointer-like semantics and you want to get raw pointer to value it points to, then you have to apply operator* (to access value) and then operator& (to retrieve address).
This is most likely usecase when you will see construct like that. It is impossible to know more without knowing what A
and related classes is: * can be overloaded to do something else and & can be overloaded too.
Assuming A
is kind of object, then probably *
is an operator.
If A
is say iterator, then a
could be the address of current element where the iterator points.
Here is the explanation which i learnt in my college. & = "address of"
&*A = from left to right "Address of , Value at A".