473,503 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using the STL Containers

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.

Jul 23 '05 #1
2 1180
"Brent Ritchie" <br**********@personainternet.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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
Jul 23 '05 #2

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

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6554
by: wtnt | last post by:
Hello, I've been using the STL libraries for some time, but still don't know the ins and outs of its implementation. Could this be because there's more than 1 implementation? Does anyone know of...
0
2089
by: Happosai | last post by:
I am attempting to convert some content layout on a site that I manage from using a table to using CSS, but I cannot figure out how to do it. The following HTML is a simplification of what I am...
6
2318
by: alexhong2001 | last post by:
Does "std::sort" work only with sequence containers, not associative containers at all? Among sequential containers, can it be used with "list", "queue" and other sequence containers besides...
13
4953
by: Dylan | last post by:
I'd like to compare two containers. They should be considered equivalent if both containers have the same number of elements with the same values, no matter what order the values are in. For...
18
3021
by: Matthias Kaeppler | last post by:
Hi, in my program, I have to sort containers of objects which can be 2000 items big in some cases. Since STL containers are based around copying and since I need to sort these containers quite...
4
3664
by: Erik Hendrix | last post by:
Hi, I have a quick question, when one sets the prefetch size = extent size, then when doing a backup we will have 1 agent (db2bm) doing the reads. If we have prefetch size a multiple of extent...
2
1057
by: Bob Beauchaine | last post by:
I found out the hard way that I can't include an STL container in a form object, and spent a half day trying to locate authoritative documentation on exactly why. So far I've concluded from...
2
1581
by: bob | last post by:
Hi, Given: 1) Custom containers that have nothing to do with vector, deque, list, map etc, 2) a custom version of new and delete defined for these containers, customNew and customDelete,...
11
16543
by: Grischa Brockhaus | last post by:
Hi, I'm trying to produce a div layout containing a header on the top with fixed height, a footer on the bottom using fixed height and a content layer using what's left of the browsers window. ...
35
2788
by: dragoncoder | last post by:
Just a simple theoritical question to the experts. What was the rationale behind making STL containers follow copy semantics rather than reference semantics. References almost always make things...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
382
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.