Re: [gtkmm] Obtaining a pointer to an object



Miller, Ryan D wrote:
Is there a way to obtain a pointer from an object to other objects that have been created by the same parent (without passing the pointers as arguments)? I need to obtain a reference to an object in order to use sigc::mem_fun to connect a signal to another class. Both of the classes have the same parent window. I was looking at the get_toplevel and get_ancestor calls, but I don't think they'll work. Does anyone have any suggestions or some short example code?


Usually you'd do that by passing a reference to the parent object as a constructor argument to the child object, and then have the child object ask the parent object for references to other objects. Something like:

class ChildA;
class ChildB;

class Parent
{
    ChildA* child_a;
    ChildB* child_b;

    Parent() {
        child_a = new ChildA(*this);
        child_b = new ChildB(*this);
    }
    ChildB& getB() {
        return *child_b;
    }
};

class ChildA
{
    ChildA(Parent& parent) {
        ChildB& child_b = parent.getB();  // Get reference to sibling
    }
};

--
Christer Palm



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]