Pointer of object within object itself

Is it possible to store to pointer of an object within the object itself?

struct vertex{
const vertex * vrt_ptr;
}

I'm not entirely sure this is valid. Or should I try a void pointer instead?
Yes, totally possible. Although I'm not sure why you'd do it.
You should look up the this keyword, I feel like it's what you are looking for.
except for the const it looks just like a linked list node ...
which is of course valid.

I agree, it looks like "this" is what is wanted here, but there are other places where you want this type of syntax and construct.
Last edited on
I am still quite confused about how to operate the "this" pointer.

Are you suggesting :

struct vertex{
const vertex * vrt_ptr;
}

vertex v1;

v1 -> vrt_ptr = &v1;


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

EDIT: Emboldment for clarity.
Last edited on
struct v
{
const v* thisone = this; //pointless, but legal. actually, the assignment syntax may be off.

}

v x;

if(x.thisone == &x) //I think this is true. Im a little tired right now.
cout << "hooray" << endl;

While all of the above is nonsense code, it illustrates what "this" really is.

Last edited on
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).

struct vertex{
const vertex * vrt_ptr = this;
}
> 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 ;
}
Thank you. I didn't know about the addressof() operator. That seems much more useful than some of the other examples of using pointers that I've seen.
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.
Last edited on
Topic archived. No new replies allowed.