I really need help for a excercise in C++ at the University. I have no idea how to solve it.
There is one Class with this memberfunction: void Asteroid::render()
which is also virtual
and I have to call this function from another class in the following way.
I have a template list which contains many asteroids, this is the class with the member function and the template has a function to pass over the function which should be called.
void for_each(void (*do_something)(T item));
here I have to pass the pointer to void Asteroid::render()
T in template are objects of the class Asteroid. So the final call in a third file looks like this asteroids.for_each(HERE_THE_FUNCTION_POINTER);
asteroids
is the declared List<Asteroid>
and the function comes from the template.
So I really have no idea how to solve that and it's on the last minuites :( I hope you can help me so far. All the QNA's I have looked up didnt help just random compiler errors.
PS: I'm not allowed to change any declarations..
Thanks so far.
Method to Call:
void Asteroid::render()
{
// Compute transformation matrix
computeMatrix();
// Push matrix and set transformation and scaling
glPushMatrix();
glMultMatrixf(m_transformation);
glScalef(m_scale, m_scale, m_scale);
if(m_mesh)
{
m_mesh->render();
}
glPopMatrix();
}
Method wich should call render():
template <typename T>
void List<T>::for_each(void(*do_something)(T item))
{
Node* temp = this->m_list;
while (temp->next != NULL)
{
if (this->m_list->next != NULL)
{
do_something(temp->data);
temp = temp->next;
}
}
}
Executing code part:
AsteroidField::AsteroidField(int quantity, string basePath) :
m_basePath(basePath)
{
// Generate asteroids
asteroids = List<Asteroid>();
Randomizer* random = Randomizer::instance();
for(int i = 0; i < quantity; i++)
{
TriangleMesh* mesh = TriangleMeshFactory::instance().getMesh(m_basePath + "asteroid.3ds");
float size = random->getRandomNumber(1,5);
Vertex<float> pos = random->getRandomVertex(5000);
Asteroid asteroid = Asteroid::Asteroid(mesh, pos, size);
asteroids.insert(asteroid);
}
}
AsteroidField::~AsteroidField()
{
asteroids.for_each(Asteroid::~Asteroid());
}
void AsteroidField::render()
{
// TODO: Call render for all asteroids
asteroids.for_each(*render());
}