If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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…

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.

Re: Scope- Public v Protected (Private)

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.

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.

Re: Scope- Public v Protected (Private)

Originally Posted by returnofx

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.

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

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.