Using typedef?

I'm thinking of using typedef so that I can simplify
(*iter_)->sprite.Move((*iter_)->speed,0).

I tried doing:

1
2
3
4
5
std::vector<Laser*>::iterator iter = lasers.begin();
std::vector<Spaceship*>::iterator iter_ = enemies.begin();

typedef **(std::vector<Laser*>::iterator)     ppLaser;
typedef **(std::vector<Spaceship*>::iterator) ppSpaceship;


but apparently this is wrong. I get "error: too few template-parameter-lists".

Is it impossible for me to define typedefs for something like this?
typedef's only simplify type names.

(*iter_)->sprite.Move((*iter_)->speed,0) <- there are no type names here, so there's nothing that typedef can simplify.

What do you want the desired result to me? Like how do you want that line to be simplified?
Oh...I see what you mean.

I wanted to able to avoid the (*ptr)-> business, but it's not really that big a problem. It's just a little annoying to type it like that, so I was thinking that I could somehow hide the double dereferencing with a typedef.
1
2
Laser *ptr = *iter;
ptr->sprite.Move( ptr->speed, 0 );
better? (by the way, you are violating Demeter)
Last edited on
How does that work? iter is a Laser** object, so if I create a Laser* ptr to point at a dereferenced Laser**, wouldn't I still have to dereference it twice to get at the Laser? (sorry if this doesn't make any sense, but I don't understand what you did)

Googling Demeter brought up

The goal of Demeter is to maintain a loose coupling between the
objects and the operations, so that one can make modifications to either without serious impact
on the other.


I don't really understand...I mean, in this case, since I'm iterating through a vector of Laser pointers, isn't it unavoidable to have to dereference it twice to modify the Laser?
typedef is one of my favourite C++ keywords. You can use it to clean up your long type declarations:
1
2
3
4
5
6
7
8
9
typedef std::vector<Laser*> laser_vec;
typedef laser_vec::iterator laser_iter;

laser_vec lasers;

laser_iter iter = lasers.begin();

// can't do anything about this though. A type is a type, however it is defined.
(*iter_)->sprite.Move((*iter_)->speed,0)

Last edited on
Okay, that's what I thought after Disch's comment. Thanks, Galik.
iter is a Laser** object, so if I create a Laser* ptr to point at a dereferenced Laser**, wouldn't I still have to dereference it twice to get at the Laser?
1
2
3
4
Laser *ptr = *iter; //an iterator is not a pointer, but *iter returns a Laser pointer (first dereference)
(*ptr); //that's an object Laser or a derived class
(*ptr).method(); //this 2 lines are equivalent 
ptr->method(); //second dereference 
Yes, you are dereferencing twice.

Demeter can be resumed in "only talk to your friends" (where friends are your attributes, the objects that you create, or the objects that you receive as parameter).
You have a collection of Lasers, so you can ask for methods or attributes of an Laser object.
However ptr->sprite.Move(ptr->speed,0) you ask for a method of an attribute of an Laser object. (two points in a row is a sign)
You can avoid it doing something like
1
2
3
4
5
6
void Laser::Move(){ 
  //The calculus of the movement
  sprite.Move(dx, dy); //just guessing
}

ptr->Move(); //maybe later you don't want to use a sprite, or you want to change the movement 


But remember that Demeter is just a guide, so you can break it.
Last edited on
Oh..that makes a lot of sense. Now that I look at my code, I'm wondering why I didn't do that in the first place. I declared a speed in the class, then move it out of my class to call a Move function... gah! Thanks, that clears it up.
Topic archived. No new replies allowed.