Using the STL Containers

B

Brent Ritchie

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

David White

Brent Ritchie said:
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
 
M

msalters

Brent said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top