How to check if a pointer is "valid" ?

M

Manuel

I think I can't find this on google/books because is soooooooo basic...

This function assign a listner pointer to an image widget:
------------------------------------------------------
void image::setListener(AbstractListener *listener)
{
this->listener = listener;
}
------------------------------------------------------


this function use the listner:

------------------------------------------------------
bool image::isMouseOver(GLint mouseX, GLint mouseY)
{
bool isOver = false;
if (mouseX > positionX
&& mouseX < positionX + width
&& mouseY > positionY
&& mouseY < positionY + height)
{
listener->mouseOver(mouseX, mouseY, userValue);
isOver = true;
}
return isOver;
};
------------------------------------------------------

Of course, If I call the function above, without call setListner before,
the application crash.

So I must check listner is OK.
I'm trying somewhat like:

------------------------------------------------------
bool image::isMouseOver(GLint mouseX, GLint mouseY)
{
bool isOver = false;
if (mouseX > positionX
&& mouseX < positionX + width
&& mouseY > positionY
&& mouseY < positionY + height)
{
if (listener)
{
listener->mouseOver(mouseX, mouseY, userValue);
}
isOver = true;
}
return isOver;
};
------------------------------------------------------

But it crash anyway...

Please, help...

Regards,

Manuel
 
D

Dan Cernat

Manuel said:
I think I can't find this on google/books because is soooooooo basic...

This function assign a listner pointer to an image widget:
------------------------------------------------------
void image::setListener(AbstractListener *listener)
{
this->listener = listener;
}
------------------------------------------------------


this function use the listner:

------------------------------------------------------
bool image::isMouseOver(GLint mouseX, GLint mouseY)
{
bool isOver = false;
if (mouseX > positionX
&& mouseX < positionX + width
&& mouseY > positionY
&& mouseY < positionY + height)
{
listener->mouseOver(mouseX, mouseY, userValue);
isOver = true;
}
return isOver;
};
------------------------------------------------------

Of course, If I call the function above, without call setListner before,
the application crash.

So I must check listner is OK.
I'm trying somewhat like:

------------------------------------------------------
bool image::isMouseOver(GLint mouseX, GLint mouseY)
{
bool isOver = false;
if (mouseX > positionX
&& mouseX < positionX + width
&& mouseY > positionY
&& mouseY < positionY + height)
{
if (listener)
{
listener->mouseOver(mouseX, mouseY, userValue);
}
isOver = true;
}
return isOver;
};
one cannot check to see if a pointer is valid or not. Not in standard C++.
Please, help...
in the constructor of image object, set listener pointer to zero.
 
V

Victor Bazarov

Manuel said:
I think I can't find this on google/books because is soooooooo basic...

This function assign a listner pointer to an image widget:
------------------------------------------------------
void image::setListener(AbstractListener *listener)
{
this->listener = listener;
}
------------------------------------------------------


this function use the listner:

------------------------------------------------------
bool image::isMouseOver(GLint mouseX, GLint mouseY)
{ [..]
listener->mouseOver(mouseX, mouseY, userValue);
> [..]
};
------------------------------------------------------

Of course, If I call the function above, without call setListner before,
the application crash.

So I must check listner is OK.

Simple. Initialise 'listener' with 0 in the constructor. Check for its
value before using it:

if (listener)
listener->mouseOver(...
I'm trying somewhat like:

------------------------------------------------------
bool image::isMouseOver(GLint mouseX, GLint mouseY)
{
bool isOver = false;
if (mouseX > positionX
&& mouseX < positionX + width
&& mouseY > positionY
&& mouseY < positionY + height)
{
if (listener)
{
listener->mouseOver(mouseX, mouseY, userValue);
}
isOver = true;
}
return isOver;
};

Not if you actually initialise 'listener' to 0. Otherwise, it's left
uninitialised, and using an uninitialised pointer causes undefined
behaviour.

V
 
A

Artie Gold

Manuel said:
I think I can't find this on google/books because is soooooooo basic...

No, you can't find this on Google or in books because it's impossible[1]!
This function assign a listner pointer to an image widget:
------------------------------------------------------
void image::setListener(AbstractListener *listener)
{
this->listener = listener;
}
------------------------------------------------------


this function use the listner:

------------------------------------------------------
bool image::isMouseOver(GLint mouseX, GLint mouseY)
{
bool isOver = false;
if (mouseX > positionX
&& mouseX < positionX + width
&& mouseY > positionY
&& mouseY < positionY + height)
{
listener->mouseOver(mouseX, mouseY, userValue);
isOver = true;
}
return isOver;
};
------------------------------------------------------

Of course, If I call the function above, without call setListner before,
the application crash.

So I must check listner is OK.
I'm trying somewhat like:

------------------------------------------------------
bool image::isMouseOver(GLint mouseX, GLint mouseY)
{
bool isOver = false;
if (mouseX > positionX
&& mouseX < positionX + width
&& mouseY > positionY
&& mouseY < positionY + height)
{
if (listener)
{
listener->mouseOver(mouseX, mouseY, userValue);
}
isOver = true;
}
return isOver;
};

The only suggestion I would have would be to initialize the listener to
NULL (0) -- otherwise there's no way to distinguish between
uninitialized garbage and a valid value.

HTH,
--ag

[1] There may be a way to perform a `sanity check' in a platform
specific way -- but even that would not be reliable (and nothing *I*
would use for production code).
 
M

Manuel

in the constructor of image object, set listener pointer to zero.


It works, THANKS GUYS!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top