Scope- Public v Protected (Private)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Scope- Public v Protected (Private)

  1. #1
    Join Date
    Feb 2012
    Posts
    6

    Scope- Public v Protected (Private)

    I am more after education here rather than solving the issue, because this flies against my understanding of public and protected (and private), or I am missing a nuanced problem here. Either way I would appreciate an expert’s opinion.

    The project is irrelevant, but effectively I am using a class to deal with an element of drawing to a CView via a public function. When I start the application it creates a new instance of the class (I use a pointer to reference it). The class constructor populates the graphical data, and then the draw function is successfully called. Ostensibly the draw function runs through an array of points to generate the required lines on the CView – this works fine. The array is protected.

    I also call the public draw function following a window resize, the function is called correctly but the points aren’t draw. The problem seems that even though the public draw function uses protected members of the same class, some of them seem to be out of scope (the postNum is not correctly reference, it appears out of scope). The array (also protected) is fine, I believe this is because arrays are always referenced as pointers behind the scene. But I am perplexed why the class function cannot access its own protected members. If I make the offending items public, it works just fine.

    I am either missing something fundamental with public and protected (and by inference private) or I am missing a scope issue with my class pointer (but that wouldn’t help me explain why some of the other members remain valid).

    I hope the following code snippets show enough for you to telling me where I am being stupid…

    class CTerrain : public CObject
    {
    public:
    CTerrain(HWND hWnd);
    ~CTerrain() {};
    void Draw(CDC* pDC);
    COLORREF colourErase,colourDraw;

    protected:
    RECT clientRect;
    POINT terrain [2000]; // terrain data
    int postNum; // number of posts
    float postSpacing; // pixels between the posts

    private:
    float getRand (int min, int max);
    void blendTerrain (int x1, int x2);

    };

    void CTerrain:: Draw(CDC* pDC)
    {
    pDC->FillSolidRect(&clientRect,colourErase);

    pDC->MoveTo(terrain[0].x,terrain[0].y);

    for (int i=1; i<=postNum; i++)
    {
    pDC->LineTo(terrain[i].x,terrain[i].y);
    }
    }


    CView Class .cpp

    This bit always works

    void CTrajectoryView::startProcess()
    {
    Terrain=&(CTerrain (GetSafeHwnd()));
    Terrain->Draw(GetWindowDC());
    }


    This bit will only work if I make postNum public vice protected in Terrain.h

    void CTrajectoryView::OnDraw(CDC* pDC)
    {
    CTrajectoryDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
    return;

    if (StartUp)
    startProcess();
    else
    {
    Terrain->Draw(pDC);
    }
    }

  2. #2
    Join Date
    Apr 1999
    Posts
    27,422

    Re: Scope- Public v Protected (Private)

    First, please use code tags when posting code:
    Code:
    class CTerrain : public CObject
    {
        public:
             CTerrain(HWND hWnd);
            ~CTerrain() {};
             void Draw(CDC* pDC);
             COLORREF colourErase,colourDraw;
        protected:
             RECT clientRect;
             POINT terrain [2000];	
             int postNum;	
             float postSpacing;	
        private:
            float getRand (int min, int max);
            void blendTerrain (int x1, int x2);
    };
    
    void CTerrain:: Draw(CDC* pDC)
    {
        pDC->FillSolidRect(&clientRect,colourErase);
        pDC->MoveTo(terrain[0].x,terrain[0].y);
        for (int i=1; i<=postNum; i++)
        {
             pDC->LineTo(terrain[i].x,terrain[i].y);
        }
    }
    What I don't understand is this:
    Code:
    //This bit always works
    void CTrajectoryView::startProcess()
    {
       Terrain=&(CTerrain (GetSafeHwnd()));
       Terrain->Draw(GetWindowDC());
    }
    Can you explain what youi're attempting to do with that line in red? What is "Terrain"?

    Something isn't right in those two lines. The GetSafeHwnd() function returns an HWND. Then you're creating a temporary CTerrain object from this HWND. Then you're assigning the address of this temporary to Terrain. The temporary object is gone after you've done all of that. So that second line with the Draw() function inovkes undefined behaviour, since the temporary is gone, but your Terrain pointer is still pointing to it.

    In other words, your function that you say "works" really doesn't work. If it works it's only by luck (the temporary object still has its remnants in memory only by chance, and your code is erroneously accessing this memory). So before doing anything else, you should address these issues, and if posting code:

    1) Use code tags.

    2) Post a more complete example. Things such as "Terrain" popping up out of nowhere leads to more questions.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Apr 1999
    Posts
    27,422

    Re: Scope- Public v Protected (Private)

    Quote Originally Posted by returnofx View Post
    This bit will only work if I make postNum public vice protected in Terrain.h
    If you're saying that the program behaves differently when you move variables around, then I can bet that you're corrupting memory in some way.

    Anytime a "fix" is done by merely moving variables around, that is a tell-tale sign that the problem is memory corruption on your part. An example of this is what I initially posted. When you now move variables around, all you're doing is moving the memory corruption bug to another area in your program.

    So by moving a variable from protected to public or whatever you did doesn't fix anything -- worse than that, it masks whatever bug you have, and now you have a program that is unstable without you knowing about it.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; October 10th, 2012 at 03:36 AM.

  4. #4
    Join Date
    Feb 2012
    Posts
    6

    Re: Scope- Public v Protected (Private)

    Disch

    I figured it out the other day, I feel very stupid.... basics! Terrain is a global pointer but I set it to point to an object that is created locally, hence it becomes a bad ptr once the object goes out of scope. But thank you for taking the time to reply.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,422

    Re: Scope- Public v Protected (Private)

    Quote Originally Posted by returnofx View Post
    Disch

    I figured it out the other day, I feel very stupid.... basics! Terrain is a global pointer but I set it to point to an object that is created locally, hence it becomes a bad ptr once the object goes out of scope.
    Did you fix the error that I pointed out? You are creating a temporary object that gets destroyed as soon as that statement is completed. It is actually worse than a local object, as a local object has "life" after it's created (but dies as soon as the functional block is executed, while a temporary object is destroyed as soon as that line of code is finished.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; October 10th, 2012 at 03:35 AM.

  6. #6
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,010

    Re: Scope- Public v Protected (Private)

    You should know that the line of code that Paul highlighted isn't even valid C++. It's a known flaw in the VC++ compiler that it doesn't recognize this as an error.
    If I compile this program in VC++, I get no errors
    Code:
    struct Test {};
    int main()
    {
      Test* t = &Test();
    }
    But using the Comeau online compiler, I get
    Code:
    Comeau C/C++ 4.3.10.1 (Oct  6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
    Copyright 1988-2008 Comeau Computing.  All rights reserved.
    MODE:strict errors C++ C++0x_extensions
    
    "ComeauTest.c", line 5: error: expression must be an lvalue or a function designator
        Test* t = &Test();
                   ^
    
    1 error detected in the compilation of "ComeauTest.c".
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center