This is a pointer to the instance of the object that a member function is being defined for. It would save that assignment operation that you are performing, but only if you are using it within a member function.
I think I am starting to get this. To clarify my objective, (NOTE: I am obviously a noob at this), is to use the pointer to help sorting and accessing geometric data and to give me the ability to change the vertex ID (a cleanup function, so to speak), before sending it to a save file (hopefully an XML file if I can hurry up and learn the boost XML library).
> Is it possible to store to pointer of an object within the object itself?
It is possible:
1 2 3 4 5 6 7 8 9 10 11 12 13
struct vertex
{
vertex() : pointer_to_this_vertex(this) {} // initialise in the constructor
const vertex* const pointer_to_this_vertex ;
// copy/move constructor, copy/move assignment
// don't copy or move pointer_to_this_vertex
// (explicitly initialise it with 'this' in the constructors
// and do not assign to it in the assignment operators)
// ...
};
It is also completely useless (except for the (fundamentally unsound) edge-case of determining the address of an anonymous temporary). To be able to access this non-static member of the vertex, we should already have a vertex object; and if we have that object, to get its address we do't need to access any member.
1 2 3 4 5 6 7
void foo( const vertex& v )
{
const vertex* const pointer_to_vertex = std::addressof(v) ;
// we do not need this
// const vertex* const pointer_to_vertex2 = v.pointer_to_the_vertex ;
}
Usually the address-of a particular object is written using the built-in address-of operator: const vertex* const pointer_to_vertex = &v ;
Instead of the function std::addressof.
std::addressof has a place in generic code - returning the address of its argument even when that argument supplies an alternative meaning for &v.
Part of the power of the language comes from the potential for supplying those alternate meanings, but there are cases (like here) where such alternate meanings would do nothing except cause problems.