// define variable
CScribbleDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// The view delegates the drawing of individual strokes to
// CStroke::DrawStroke().
CTypedPtrList<CObList,CStroke*>& strokeList = pDoc->m_strokeList;
POSITION pos = strokeList.GetHeadPosition();
1.
////////////////
//因為假設strokeList有很多class所組成的話,這樣子做的話
//我就可以依據不同的class,來呼叫不同的DrawStroke.....
//可是這樣寫run的結果是錯誤,且系統會要我強制離開程式
//請問各位高手能幫幫我嘛??
if ( (
strokeList.GetNext(pos)->IsKindOf( RUNTIME_CLASS(CStroke) )
) != 0 )
{
CStroke* pStroke = strokeList.GetAt(pos) ;
pStroke->DrawStroke(pDC);
}
2.
///////////////////////
//這樣寫run的結果是對的就正確,可是我卻不想要這樣,
//因為假設strokeList有很多class所組成的話,這樣子做
//就太沒彈性了....
CStroke* pStroke = strokeList.GetNext(pos);
if ( (
pStroke->IsKindOf( RUNTIME_CLASS(CStroke) )
) != 0 )
{
pStroke->DrawStroke(pDC);
}
謝謝...
--
※ Origin: 楓橋驛站(bbs.cs.nthu.edu.tw) ◆ From: 210.66.158.194
>1.
>////////////////
>//因為假設strokeList有很多class所組成的話,這樣子做的話
>//我就可以依據不同的class,來呼叫不同的DrawStroke.....
>//可是這樣寫run的結果是錯誤,且系統會要我強制離開程式
>//請問各位高手能幫幫我嘛??
>
>
> if ( (
> strokeList.GetNext(pos)->IsKindOf( RUNTIME_CLASS(CStroke) )
> ) != 0 )
> {
> CStroke* pStroke = strokeList.GetAt(pos) ;
> pStroke->DrawStroke(pDC);
> }
問題可能是這樣的: GetNext(pos) 執行完畢後會將 pos 所指的
object 傳出來, 同時會將 pos 的值指向下一個 object, 也就是說
你程式裡 GetNext(pos) 與 GetAt(pos) 這兩行裡的 pos 其實是指向
不同的 object 的, 且 GetAt(pos) 的 pos 可能為 NULL。
>2.
>///////////////////////
>//這樣寫run的結果是對的就正確,可是我卻不想要這樣,
>//因為假設strokeList有很多class所組成的話,這樣子做
>//就太沒彈性了....
>
>CStroke* pStroke = strokeList.GetNext(pos);
> if ( (
> pStroke->IsKindOf( RUNTIME_CLASS(CStroke) )
> ) != 0 )
>{
> pStroke->DrawStroke(pDC);
>
>}
這個寫法是正確的寫法, 你覺得哪裡不妥嗎?
四眼的王蟲
--
生命是在黑暗中閃爍的光
用 GetNext(pos) 後, pos 會指到下一個 object
> ) != 0 )
> {
> CStroke* pStroke = strokeList.GetAt(pos) ;
^^^^^^^^^^^^^^^^^^^^^
pStroke 已不是上一個 checking 的 object.
> pStroke->DrawStroke(pDC);
> }
建議你的 DrawStroke 做成 virtual function