0

PROBLEM:

I want to set up a system of elements, while at the same time keeping pairs of these elements that I want to access and modify easily (add more).

At the same time, I want to keep the complexity to a minimum, and not implement everything in one class.

I decided to store the indices to the pairs using a struct, which can handle a large amount of the complexity of calculations among B's.

class A {
    class B_pair {
        int idx_of_B[2];

    public:
        B get_B_value_1(); // how to access B's?
        B get_B_value_2();
        // a lot more member functions
    };

public:
    std::vector<B>  B_values;
    std::vector<B_pair>  special_B_pairs;
    // a lot more stuff
};

However, I have the problem that I cannot access the B values from within the B_pair class.

FAILED SOLUTION 1

class A {
    A() { B_pair::outer = this; }
    class B_pair {
        static A *outer;
        void init_outer(A *o);
    };
}

I used a static member because I have a lot of B_pair objects, and the extra memory of a pointer/reference in each is significant.

This works fine. However, if I have more than one A objects at the same time, then it is problematic, as the outer pointer ends up pointing at the last created A object.

FAILED SOLUTION 2

Another approach I took was storing pointers.

class A {
    class B_pair {
        B *items[2];
    };
    std::list<B>  B_values; // I need random access iterators!!
};

However, this means that I need to use more inefficient data structures than std::vector, which invalidates its iterators when it expands.

QUESTION

How could I access the members of the outer class A from the inner class B, or something similar to make this work? Ideally, I would like something similar to the first solution, but with one instance of A *outer for each A object.

Note that THIS IS POSSIBLE IN JAVA!!

P.S. At the moment I am using the Singleton design pattern, and kept the static pointer. Not a big fan of this solution.

Kostas
  • 4,061
  • 1
  • 14
  • 32
  • You need to store a regular pointer in each `B_pair` object. If these were POD classes you could use `offsetof` to get a pointer to `A` from `&B_values`, but that doesn't work for non-POD classes. – Barmar Jun 08 '18 at 02:08
  • @Barmar I can't access `&B_values` though. My inner class only has an index to an element in `B_values`. Btw why would this not work for non-POD? Do member functions affect size in any way? – Kostas Jun 08 '18 at 02:14
  • 2
    https://stackoverflow.com/questions/1129894/why-cant-you-use-offsetof-on-non-pod-structures-in-c?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Barmar Jun 08 '18 at 02:15
  • if you're asking for architectural direction, make `B` a "dumb" object that is managed by `A` where access and modification of `B`s is done through calls on an `A` – kmdreko Jun 08 '18 at 07:12
  • @vu1p3n0x Sure, but this obfuscates code... `objectA.first(objectA.special_B_pairs[i])` instead of `objectA.special_B_pairs[i].first()` ? – Kostas Jun 08 '18 at 13:10
  • 1
    It looks like `B_pair`s represent a link, you could, again, make it dumb and make it clear it only has `B` "indexes" that can be accessed through an `A` (i.e. `objectA.getB(objectA.special_B_pairs[i].first)`). That's how many graph-like structures are done. – kmdreko Jun 08 '18 at 14:13

0 Answers0