CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    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.

  2. #2
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    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*.

  3. #3
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570
    Thank you very much gstercken.

    Mitesh.

  4. #4
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570
    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.

  5. #5
    Join Date
    Jun 2003
    Location
    Gjøvik, Norway
    Posts
    204
    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.

  6. #6
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    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."

  7. #7
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570
    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
  •  





Click Here to Expand Forum to Full Width

Featured