headerfile.h
class A
{
cv::Mat depthimagemouse;
std::string m_winname;
public:
A(const std::string &winname, const cv::Mat depth_clean);
static void onMouse( int evt, int x, int y, int flags, void* param );
};
cppfile.cpp
A::A(const std::string &winname, const cv::Mat depth_clean)
: m_winname(winname), depthimagemouse(depth_clean)
{
//something..
}
void A::onMouse( int event, int x, int y, int flags, void* param )
{
//Here I want to use depthimagemouse member variable (and other members..)
}
My question is how can I use depthimagemouse variable in onMouse method?
I'd be surprised if the library didn't explain this somewhere in its documentation, but anyway. This is standard procedure when you're using callbacks that don't support member functions, but you still need a way to access member data. So, you do the following:
param
(or a member thereof) when registering the callback. So, you can do it this way:
auto &that = *static_cast<A *>(param); // <= C++03: use A &that
// Cast to const A if you want, or use a pointer, or etc.
std::cout << that.depthimagemouse << std::endl;
Or it's often nicer syntactically to immediately despatch to a member function and let it do everything:
static_cast<A *>(param)->doRestOfStuff(evt, x, y, flags);
// Include a const in the cast if the method is const
Or anywhere in between.
depthimagemouse
is an instance member, means that each A
instance (object if you prefer) has its own depthimagemouse
. Your onMouse
method is a static one, means that it is not related to any particular given instance but to all instances, and thus it's nonsense to think about accessing depthimagemouse
without specifying which instance you are interested in.
Without any more information about onMouse
and your model it is hard to tell you what to do. May be param
can be used to specify an instance of A
that the static method will receive? In that case, a cast can be used to get the instance back inside the method: A *anInstance = static_cast<A *>(param);
with which you can then play: anInstance->depthimagemouse
(see that we are talking about the depthimagemouse
of a given instance?), etc.
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.