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