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);
}
}