CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Hybrid View

  1. #1
    Join Date
    Feb 2004
    Location
    HK
    Posts
    2

    accessing variable that belongs to aother class

    Hello
    I have a member variable in the class CSpanaView,called ZoomIndicator. I want to access that variable in aother class CFastZoom. Below is the code by me to do above task
    CFastZoom::OnCancel()
    {
    CSpanaView *ptSpana;
    ptSpana->ZoomIndicator=FALSE;
    }

    but there is a memory exception when i run the code above

    ZoomIndicator is a bool

    Please help me!Thank You!

  2. #2
    Join Date
    Jun 2003
    Location
    Cochin
    Posts
    364

    Re: accessing variable that belongs to aother class

    CFastZoom::OnCancel()
    {
    CSpanaView *ptSpana;
    ptSpana->ZoomIndicator=FALSE;
    }

    You cannot access a private member variable in CSpanaView class like this.
    You can access ZoonIndicator through the member functions of CSpanaView if it is a private member variable.
    Last edited by joscollin; February 27th, 2004 at 03:40 AM.

  3. #3
    Join Date
    Feb 2004
    Location
    HK
    Posts
    2
    Thank You joscollin

  4. #4
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    You need to assign a valid address to *ptSpana before you can dereference it. Since you didn't assign any value to *ptSpana in its declaration, it is pointing to an invalid address. As a result, when you dereference it, memory exception occurs.

  5. #5
    Join Date
    Aug 2002
    Location
    United States
    Posts
    729
    well prior to even touching the private / public issue i will point out that

    CSpanaView *ptSpana;
    ptSpana->ZoomIndicator=FALSE;


    will always be invalid because you never actually create an object or otherwise assign the pointer. without more info on your code though i wont provide any more insight on how you should solve this except to say that you shouldn't access even public members VARIABLES in my opinion (i consider public variables bad design, there should be a member function that returns/sets the variable or something) but that's just my opinion and there are exceptions


    -edit-
    i see kheun has just posted the same point while i was typing it up. my apologies.
    Last edited by filthy_mcnasty; February 27th, 2004 at 03:47 AM.

  6. #6
    Join Date
    Jun 2003
    Location
    Cochin
    Posts
    364
    Yes. What Kheun said is correct.
    You can allocate memory for an object and assign its address to
    ptSpana.

    CSpanaView *ptSpana = new CSpanaView(......

    It's better ZoomIndicator to be a private variable and access it through your operator functions in CSpanaView class.
    Last edited by joscollin; February 27th, 2004 at 03:53 AM.

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