|
-
April 4th, 2004, 07:26 AM
#1
Some problem about scope.
Code:
CWordBox *pReturn;
I am calling CGrid::Left(CWordBox* pWordBox, CWordBox* pReturn)
as below:
switch(nChar)
{
case VK_LEFT:
{
m_pGrid->Left(this, pReturn);
if(pReturn != NULL)
{
pReturn->SetFocus();
}
}
break;
The implementation of CGrid::Left is as below
int CGrid::Left(CWordBox* pWordBox, CWordBox* pReturn)
{
CPoint Point;
Point.x = pWordBox->m_Rect.TopLeft().x - 10;
Point.y = pWordBox->m_Rect.TopLeft().y + 10;
if(pWordBox->m_Rect.TopLeft().x == 0)
return BORDER;
else
{
pReturn = FindBox(Point);
if(pReturn == NULL || !pReturn->m_bActive)
{
pReturn = NULL;
return NULL;
}
else
return 1;
}
return NULL;
}
When I debug and step into the function CGrid::Left, in one case it returns 1 if pReturn is not equal to NULL and pReturn->m_bActive is TRUE.
But when I reach here:
m_pGrid->Left(this, pReturn);
if(pReturn != NULL)
{
pReturn->SetFocus();
}
pReturn is always NULL even when pReturn was valid pointer inside CGrid::Left(...).
How can I solve this problems.
Please Help
Any help will be appreciated greatly.
-
April 4th, 2004, 08:02 AM
#2
Your problem has nothing to do with scope, but with the required level of indirection. CGrid::Left() works on a copy of the pointer you pass to it (as always when you pass an argument by value). So inside CGrid::Left(), you will be able to modify what pReturn points to, but not the value of the external pReturn itself. In order to modify the pointer, you have to pass it by reference, that is, either as a CWordBox**, or better, as a reference to a CWordBox*.
-
April 4th, 2004, 08:56 AM
#3
Thank you very much gstercken.
Mitesh.
-
April 4th, 2004, 09:43 AM
#4
Every things works perfect but I would like to ask a question.
I am now using referenct to the pointer instead of pointer to pointer.
Previously I declared the function like below:
int Left(CWordBox* pWordBox, CWordBox* pReturn = NULL);
Now I declare it like this:
int Left(CWordBox* pWordBox, CWordBox*& pReturn);
How can I provide a default value for for the second case?
When I declare
int Left(CWordBox* pWordBox, CWordBox*& pReturn = NULL);
it gives some error.
Mitesh.
-
April 4th, 2004, 02:31 PM
#5
You're trying to set the reference to the pointer to NULL. C++ doesn't allow references to be NULL.
Last edited by Assmaster; April 4th, 2004 at 02:38 PM.
-
April 4th, 2004, 03:04 PM
#6
In other words, if you want the possibility that pReturn is NULL
use
Left(CWordBox* pWordBox, CWordBox** pReturn = NULL);
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
April 5th, 2004, 01:07 PM
#7
When I tried to use (**) I got a lot of errors.
Could you please give me an example of using (**). Modifying the code that I have given would be perfect.
Mitesh.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|