zell zell - 3 months ago 10
C++ Question

What does a=&*A mean in C++ (in which A is a pointer)?

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
?

Answer

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...
}