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?
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().
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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.