-1

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?

zell
  • 9,830
  • 10
  • 62
  • 115

4 Answers4

9

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...
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
3

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.

Revolver_Ocelot
  • 8,609
  • 3
  • 30
  • 48
2

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.

Nick
  • 9,962
  • 4
  • 42
  • 80
-1

Here is the explanation which i learnt in my college. & = "address of"

  • = "value at"

&*A = from left to right "Address of , Value at A".