Reputation: 1885
I am currently trying to use the "this" pointer to pass a pointer to a function:
void GameObject::process_events()
{
std::vector<ObjectEvent*>::iterator it;
for (it = events.begin(); it != events.end(); it++)
(*it)->process(this);
}
Class ObjectEvent
{
private:
bool* trigger;
void (*response)(GameObject*);
public:
process(GameObject* obj)
{
if (*trigger)
response(obj);
}
};
But I get an error:
No matching function call to 'ObjectEvent::process(GameObject* const)'
What could be the problem?
Upvotes: 0
Views: 165
Reputation: 2340
If the member function you're returning this
from or using this
in is const, then it will be a const pointer. If the member function is not declared const, the pointer won't be either.
void GameObject::process_events()
{
// ...
process(this); // 'this' is NOT a const pointer
}
void GameObject::process_events() const
{
// ...
process(this); // 'this' IS a const pointer
}
Upvotes: 1
Reputation: 361605
Judging by your error message, process_events() appears to actually be a const function.
void GameObject::process_events() const
{
process(this);
}
If so, then this
is a const pointer and process() must take a const GameObject *
. Otherwise process() could modify the point that gets passed to it, which violates process_events's promise not to modify this
.
void process(const GameObject* obj);
Alternatively, remove the const modifier from process_events().
Upvotes: 5