Using the STL Containers

Discussion in 'C++' started by Brent Ritchie, Apr 7, 2005.

  1. Hello,

    I have a problem that's really getting to me. I have an
    std::vector, and it holds pointers to classes. From these classes I
    need a refrence to the object that holds the vector, because it has the
    information needed to perform certain functions on the objects in the
    std::vector. for clarity I have an example.

    class A
    {
    bool moveOK(B *b1, int x, int y);
    std::vector<b*> listOfBClass;
    };

    class B
    {
    int x, y;
    void move(int x, int y);
    A *aRefrence; // Causes an error
    };

    B::move(int x, int y)
    {
    // how can I send the moveOK message?
    }

    As you can see I will get a bunch of errors saying stuff is undefined.
    (All these classes are in different files, so I guess it creates a
    circular dependency.) What I want to happen is I want to call the
    moveOK method from within class B. The moveOK method looks through the
    vector and finds if it is possible for the calling object to move, if
    it returns true then the rest of the movement code is executed. Is
    there a way to do this that is standard? I really don't want to resort
    to hackish code, like using dynamic_cast when there is another way.

    Also, if there is no "good" way to make this work can anyone suggest
    another route? I can suppply more detail at request.

    Thanks.
    Brent Ritchie, Apr 7, 2005
    #1
    1. Advertising

  2. Brent Ritchie

    David White Guest

    "Brent Ritchie" <> wrote in message
    news:...
    > Hello,
    >
    > I have a problem that's really getting to me. I have an
    > std::vector, and it holds pointers to classes. From these classes I
    > need a refrence to the object that holds the vector, because it has the
    > information needed to perform certain functions on the objects in the
    > std::vector. for clarity I have an example.
    >
    > class A
    > {
    > bool moveOK(B *b1, int x, int y);
    > std::vector<b*> listOfBClass;
    > };
    >
    > class B
    > {
    > int x, y;
    > void move(int x, int y);
    > A *aRefrence; // Causes an error
    > };
    >
    > B::move(int x, int y)
    > {
    > // how can I send the moveOK message?
    > }
    >
    > As you can see I will get a bunch of errors saying stuff is undefined.
    > (All these classes are in different files, so I guess it creates a
    > circular dependency.) What I want to happen is I want to call the
    > moveOK method from within class B. The moveOK method looks through the
    > vector and finds if it is possible for the calling object to move, if
    > it returns true then the rest of the movement code is executed. Is
    > there a way to do this that is standard? I really don't want to resort
    > to hackish code, like using dynamic_cast when there is another way.


    There is a bunch of standard algorithms to iterate over collections and take
    certain actions, but it's not clear enough to me what you want to do.

    > Also, if there is no "good" way to make this work can anyone suggest
    > another route? I can suppply more detail at request.


    The code you posted is pretty sloppy. I've fixed some errors and omissions
    and guessed what you intended in a few places.

    #include <vector>

    class B;

    class A
    {
    public:
    bool moveOK(B *b1, int x, int y);
    private:
    std::vector<B*> listOfBClass;
    };

    bool A::moveOK(B *b1, int x, int y)
    {
    // insert code
    }

    class B
    {
    int x, y;
    void move(int x, int y);
    A *aRefrence; // Causes an error
    };

    void B::move(int x, int y)
    {
    aRefrence->moveOK(this, x, y);
    }

    DW
    David White, Apr 7, 2005
    #2
    1. Advertising

  3. Brent Ritchie

    msalters Guest

    Brent Ritchie wrote:
    > Hello,
    >
    > I have a problem that's really getting to me. I have an
    > std::vector, and it holds pointers to classes. From these classes I
    > need a refrence to the object that holds the vector, because it has

    the
    > information needed to perform certain functions on the objects in the
    > std::vector.


    You cannot, in general. Pointers are a one-direction interface.
    If A has a pointer to B, you can get from A to B. However, you cannot
    go back. This makes C++ efficient: a char doesn't need to keep
    information about all objects that point to that char, so it can be
    just 8 bits.

    Now, if you do need a link back from B to A, you should add it
    yourself. The basic concept is easy. You give B an method inA(A&)
    which you call whenever a B is added to A. Inside B you keep track
    of the A object(s) which called this method.

    HTH,
    Michiel Salters
    msalters, Apr 7, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    272
    Default User
    Sep 22, 2005
  2. Replies:
    8
    Views:
    847
  3. Replies:
    7
    Views:
    553
    Pete Becker
    Jan 25, 2008
  4. Andrey Vul
    Replies:
    6
    Views:
    569
    James Kanze
    Oct 22, 2009
  5. Sebastian Mach
    Replies:
    5
    Views:
    311
Loading...

Share This Page