SOLVED, C++ vector of vectors hell

Started by
25 comments, last by darenking 18 years, 9 months ago
That's all working swimmingly.

The final, final thing, and probably a very simple thing, is that I need to pass this second vector, the one that exists as a data member of Character, to a third object, Actor (so that the actors take on the characteristics of the character).

So, forgetting about the previous stuff in World with the 2D vector... the Character object has this data member:

std::vector<int> m_AnimCycle;

All I want to do is send it in some way, by pointer or reference, to Actor. Bear in mind:

1 The values in the vector won't need to change, they're just set up at the start of the program, so it doesn't have to be received as a vector. It would nice and neat if it could be received as though it were an array, and could be just accessed with squared brackets.

2 The Actor object will already know how many values it contains so no probs with boundaries.

I want to do it the simplest way with the simplest syntax, unless of course there's a very good reason why not. Can I just in some way send the memory address of the start of the vector and receive it as though it were the start of an array?
Advertisement
Quote:Original post by darenkingI want to do it the simplest way with the simplest syntax, unless of course there's a very good reason why not. Can I just in some way send the memory address of the start of the vector and receive it as though it were the start of an array?


const int* Character::getAnimCycle(void) const { return &m_AnimCycle[0];}


simple and conforming.


HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
That compiles big time. But how do I store it and access the values in the receiving object, using squared brackets? What's the best way to store it as a data member?

Let's say I want to access the 3rd int in the array/vector...
Quote:Original post by darenking
That compiles big time. But how do I store it and access the values in the receiving object, using squared brackets? What's the best way to store it as a data member?

Let's say I want to access the 3rd int in the array/vector...


const int *p = myCharacter.getAnimCycle();if( p[2] == 42)//meaning of life the universe and everthign found!  hooray();
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
Quote:Original post by darenking

So, forgetting about the previous stuff in World with the 2D vector... the Character object has this data member:

std::vector<int> m_AnimCycle;

All I want to do is send it in some way, by pointer or reference, to Actor. Bear in mind:

1 The values in the vector won't need to change, they're just set up at the start of the program, so it doesn't have to be received as a vector. It would nice and neat if it could be received as though it were an array, and could be just accessed with squared brackets.


Pass the vector in by const reference. The vector *can* be treated "as though it were an array"; it overloads operator[]. The const correctness will protect you from changes, and passing a reference avoids copying the data.

You want to arrange for the Character to call an Actor method in this case; having the Actor just plain access Character data is probably "backwards" - objects generally are supposed to be responsible for their contents, and not simply allow others to grab them for use, but instead do the useful thing with their own contents when possible - and *send* their stuff outwards otherwise (delegation). There's a notable exception - containers - but the standard library provides all the containers you're ever likely to need.

However, I'd have to see more of the two classes and get a better sense of what they "do" to tell you more.
Quote:Original post by Deyja
Quote:
main(){
int *pt = new int(50);
method(pt);
delete pt;
}
void method(int &pt){ //pass by pointer


}


It would be a challenge to be more wrong than you. That does not compile, and is not an example of 'pass by pointer' (Your terminology is terrible as well). It is almost but not quite an example of passing a reference. What you meant was this:

void foo(int* iamanint) {/*...*/}int main(){   int an_int;   foo(&an_int);   return 0;}



lol yea i just realized i had it backwards
Quote:Original post by Zahlman
However, I'd have to see more of the two classes and get a better sense of what they "do" to tell you more.


Well, the World object creates lots of Character objects, and also lots of Actor objects.

If the game were Pac-Man, there would be two Characters, the Pac-Man and the Ghost Characters, and there would be five Actors.

The World object would pass a pointer to the Ghost Character to four of the Actor objects, and a pointer to the Pac-Man Character object to the fifth Actor object. So, the five Actors would each take on the characteristics of a particular Character.

I guess this means there's a dependency between the Characters and Actors, the Actors depending on the Characters?

The Actor class has a method called CharacterPut. The World object calls this method, effectively assigning the Actor to that type of Character. The Actor::CharacterPut method looks like this:
void Actor::CharacterPut(Character* const character){	m_pCharacter = character;	m_SizeWidth = m_pCharacter->SizeWidthQuery();	m_SizeHeight = m_pCharacter->SizeHeightQuery();	m_SizeGutterSide = m_pCharacter->SizeGutterSideQuery();	m_SizeGutterTop = m_pCharacter->SizeGutterTopQuery();	m_SizeSolidWidth = m_SizeWidth - (m_SizeGutterSide*2);	m_SizeSolidHeight = m_SizeHeight - m_SizeGutterTop;	m_PosPixelLimitLeft = 0;	m_PosPixelLimitRight = ( m_pMaze->WidthQuery() * TILE_WIDTH );	m_PosPixelLimitTop = 0;	m_PosPixelLimitBottom = ( m_pMaze->HeightQuery() * TILE_HEIGHT );	m_SpeedHorzLimit = m_pCharacter->SpeedHorzLimitQuery();	m_SpeedVertLimit = m_pCharacter->SpeedVertLimitQuery();	m_pAnimCycle = m_pCharacter->AnimCycleQuery();	for ( int action = 0 ; action < ACTOR_ACTION_AMOUNT ; action++ )	{		m_AnimType[action] = m_pCharacter->AnimTypeQuery(action);		m_AnimFrameFirst[action] = m_pCharacter->AnimFrameFirstQuery(action);		m_AnimFrameLast[action] = m_pCharacter->AnimFrameLastQuery(action);	}}

Zahlman, is this an example of "backwards"?

"You want to arrange for the Character to call an Actor method in this case" - but the Characters don't know anything about the Actors. The Characters are like costumes in a way, the Actor puts on the costume. A costume doesn't know anything about who is wearing it.

I can't see it working any other way really. Unless the World could call a method in the Character object telling it which Actor wants to take on its characteristics?

This topic is closed to new replies.

Advertisement