Hi, I'm looping through objects of the same Base class, and each object in this container is comparing itself against all other objects, but I want it to avoid comparing against itself, so I'm trying to include an if statement which checks for this. Here is what I would like to do, but the syntax isn't correct.
class Base{
//base members
virtualvoid Compare() = 0;
};
class Derived{
//derived members
virtualvoid Compare(std::vector<Base*> &objects){
for(int i = 0; i < objects.size(); i++){
if(*this != *objects[i]){
//the object isn't testing against itself, carry on with code
}
}
};
//in main
std::vector<Base*> objects;
//okay so I know the code is a little redundant, but it's the basic concept
for(int i = 0; i < objects.size(); i++){
objects[i]->Compare(objects);
}
But because I'm checking Derived against Base, there is a syntax error thrown.
In the if statement, I had this
1 2 3
if(&*this != &*objects[i]){
//the object isn't testing against itself, carry on with code
}
But I'm not sure if reading the memory address is what I want or if this would even work. It didn't seem to work anyways. So are there any suggestions on how to compare two objects on a chain of inheritance?
.... which converts to a Base pointer against a base pointer (I think)?
There is no conversion to Base Pointer - the Compiler just checks to see if Derived is a descendant of Base
The compiler does this with a vtable (virtual table). Behind the scenes, each Derived class has a pointer to it's parent class - this way the compiler can follow the "Pointer Trail" until it reaches the Base class, if not then it is a compile error. That's all - no conversion. I am guessing this may be the source of confusion in your last topics?
16 17 18
//in main, the following is declared correctly
std::vector<Base*> objects; // realise it is actually a vector of derived pointers
Just wondering why you need to compare all these derived objects?
Great, thanks for clearing that up! The idea here is that using that game programming patterns link that I believe you provided in my previous thread, I'm working on different prototypes for game code design patterns. For this instance, I'm using the flyweight method to keep all the update code inside the specific object class. So for let's say collision detection, each object holds a pointer to the vector of world objects, and each object loops through the vector of objects in the world, which includes the object iterating through the loop, so I want to avoid that self-check.
I have a Base and Derived because I need a base class to hold a vector of common objects in the first place. The Base is more of a template, though I know it's nothing like an actual template.
Am a bit surprised that you are using Flyweight pattern for the update function. I understood Flyweight was used when one has zillions of objects that are very similar.
I found this, on page 5, there is a section about collision detection:
It is a variation of the event system I mentioned in the other thread.
Also the Game Patterns book mentions the Component Pattern with the Observer Pattern for collisions.
So for let's say collision detection, each object holds a pointer to the vector of world objects, and each object loops through the vector of objects in the world, which includes the object iterating through the loop, so I want to avoid that self-check.
If you do that, does it mean that every object is coupled to every other object - a many to many relationship? If so, then you should be trying to avoid that, by using the Design Patterns.
> I'm looping through objects of the same Base class,
> and each object in this container is comparing itself against all other objects,
> but I want it to avoid comparing against itself,
Thanks, the content was great, but there was no example code, so I'm not sure how to implement what I read.
Also the Game Patterns book mentions the Component Pattern with the Observer Pattern for collisions.
I actually didn't catch that. Do you remember what section you read that in?
And yeah, perhaps it's not a good choice to say I'm using the Flyweight. Each object has a pointer to a common Collision object, so they all see the same collision data.
I'm not sure if I understood your problem correctly, but if you just want to make sure that an object isn't comparing itself with itself, then a simple test like this would suffice:
1 2 3 4
if(this == &objects[i]) //If both addresses are the same, then just skip this iteration
{
continue;
}
The only way this would work is if you only have single copies of every object and you're just passing pointers around (usually the way with game programming).
If you want a more thorough test, you can overload the != operator, or the == operator
to perform some conditional testing that will help you determine whether or not an object is dealing with an 'identical' object (not the same object in memory, but perhaps an exact duplicate of it).
There are chapters about Component, Observer and Mediator Patterns.
Also, a Google search on Design patterns is worthwhile. If you have a little spare cash, the Design Patterns Book by the "Gang Of Four" could be feasible.
I also thought it might be a good idea to search for open source game project code, so you can see what they have done. Hopefully you can find one that does use actual design patterns. If you are using sfml, then try to find something that uses that, I guess.
Another avenue of research might be learning how to interpret UML diagrams, as encountered in some of the documents I linked previously.