October 6th, 2012, 09:05 AM
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
void Draw(CDC* pDC);
POINT terrain ; // terrain data
int postNum; // number of posts
float postSpacing; // pixels between the posts
float getRand (int min, int max);
void blendTerrain (int x1, int x2);
void CTerrain:: Draw(CDC* pDC)
for (int i=1; i<=postNum; i++)
CView Class .cpp
This bit always works
This bit will only work if I make postNum public vice protected in Terrain.h
void CTrajectoryView::OnDraw(CDC* pDC)
CTrajectoryDoc* pDoc = GetDocument();
Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!