A Mess of Pointers

Well, my code has reached an even more convoluted stage- I need to know how to access a particular element of a class. Sounds simple, except... Well, the object is given to the function as a pointer to the pointer for the object. From there, the element of the class is inside of a pointer to a pointer inside of an object within that passed pointer to a pointer of a class.

I think the code helps a lot:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#ifndef CONNECTOR_H
#define CONNECTOR_H
#include <memory>
#include <utility>

class Vertex;

class Connector
{
    public:
        Connector();
        Connector(Vertex**,Vertex**);
        ~Connector();
        Connector(const Connector& other);
        Connector& operator=(const Connector& other);

        const std::pair<Vertex**,Vertex**> vertex_pair;
    protected:
    private:
        unsigned int state;

};

namespace std {
    template <>
        class hash<Connector>{
        public :
        size_t operator()(const Connector &c ) const{
            return hash<Vertex**>()(c.vertex_pair.first) ^ hash<Vertex**>()(c.vertex_pair.second);
        }
    };
}
#endif // CONNECTOR_H 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef VERTEX_H
#define VERTEX_H
#include <memory>
#include <unordered_set>
#include "Connector.h"


class Vertex
{
    public:
        Vertex();
        ~Vertex();
        Vertex(const Vertex& other);
        Vertex& operator=(const Vertex& other);
        void remove_connection(Connector**);
    protected:
    private:
        unsigned int state;
        std::unordered_set<Connector**> connection_set;
};

#endif // VERTEX_H 


I need to access connection_set, through the objects within vertex_pair, in the function remove_connection. Would it be possible to do it within the vertex proper, or is another function needed to take care of it from the perspective of a connector? If another function (or better yet, the destructor proper) is needed, how would I go about doing so? These pointers hate me right now.

Now, just to specify- the hash function is related to using the Connector class as a key of an unordered_multimap in another, unrelated class. The reason why I'm doing this absurd pointer-to-pointer thing everywhere is because I can't have any vertex or connector actually storing the other- rather, they can only have pointers to the objects stored elsewhere.
Last edited on
The reason why I'm doing this absurd pointer-to-pointer thing everywhere is because I can't have any vertex or connector actually storing the other- rather, they can only have pointers to the objects stored elsewhere.


That doesn't explain why you need to use two levels of indirection.
Wait, I think I get it.
From Connector::Connector(Vertex**, Vertex**) you want to find in each Vertex::connection_set the pointer that points to this (not the pointer that is equal to this), is that it? If so, you'll need to do a linear search over connection_set.
1
2
3
4
5
Connector::Connector(Vertex **a, Vertex **b){
    for (auto p : (*a)->connection_set)
        if (*p == this)
            //...
}

This design only makes sense if Vertexes and Connectors are stored in a centralized location and you want to modify their order within that structure. Otherwise, I see no reason for the double indirection.
Yes, that is exactly it. And yes, both are stored in a centralized location (an unordered_multimap, with the connectors as keys with their respective vertices as the values), so two levels of indirection are indeed necessary.

EDIT: Wait, it is giving me an error when I try that code as the destructor for Connector with its own vertex_pair.first and vertex_pair.second elements:

D:\Users\Ispil\C++workspace\Graph Theory Analysis\src\Connector.cpp|15|error: request for member 'connection_set' in '*(Vertex**)((Connector*)this)->Connector::vertex_pair.std::pair<Vertex**, Vertex**>::first', which is of pointer type 'Vertex*' (maybe you meant to use '->' ?)|

1
2
3
for ( auto p : *(vertex_pair.first)->connection_set)
        if (*p == this)
            //... 


EDITEDIT: No, wait- figured it out. Had to do a few more pointers to get out of that nasty situation.
Last edited on
Topic archived. No new replies allowed.