Deriving from CDumpContext and << o

orik1

New member
Deriving from CDumpContext and << o

Hi. My project needs a way to be able to dump information to a dialog (inside a multiline edit box or something similar), as well as to a file. I've already implemented Dump method in all my entities and so i figured that in order to achive the above i could derive a class from CDumpContext - implement the << operator - which will stream the data into the edit box (or the file) - and in this way i don't need to deal with each entity apart. Implementing the above i got into a problem : In my Dump method i expect a CDumpContext refrence - and i'm passing refrence to a derived class of it - so when trying to use insertion operator, <<, CDumpContexts version gets called instead of the derived classes. Any ideas, workaround ?​
 

voguemaster

New member
why reference ?

use a pointer with a virtual function and you'll get the right function called it's classic
 

orik1

New member
because...

That's how it's defined in MFC (CObject): virtual void Dump(CDumpContext &dc) const; So i'm actually passing refrence to my CMyDumpContext, and the problem is that when (inside dump function) the << operator is called - the version called is the originals CDumpContext instead of my derived from it.​
 

אלדד28

New member
תבדוק שהגדרת את הפונקציה >>

בדיוק כמו שהיא מוגדרת ב - CDumpContext. אם תשים לב יש כל מיני >>operator ב-class הזו, ואולי לא מימשת את הנכונה, ולכן אתה לא מספק בעצם פונקציה וירטואלית נכונה.
 

orik1

New member
בדיוק...

יש לי רק מימוש אחד (המקבל מחרוזת), הגדרת הפונקציה זהה בדיוק לזו שב CDumpContext (הועתקה ממנה :) ), אבל ב Debug אני רואה שהוא לא נכנס לשלי אלא למקורית...
 

orik1

New member
אולי...

בגלל שמדובר ב Refernce - שאני "מדבג" - בתוך פונקציית האופרטור, ב CDumpContext אני רואה שהאובייקט המועבר הוא אכן CDumpContext ולא ה Class שאני ירשתי.. אולי בגלל ה Refrence...?
 

orik1

New member
האם יכול להיות בגלל

שבמקור (ב CDumpContext) האופרטור אינו מוגדר כ- Virtual ? ואז אינני יכול להפעיל את האופרטור במחלקה הנורשת ?
 

orik1

New member
ויש מה לעשות ?

אם אני בכל זאת רוצה שיקרא האופרטור שלי ???
 

אלדד28

New member
לא בדיוק איך שהתכוונת.

אל תירש מ-CDumpContext. תירש מ-CFile לאובייקט משלך שיידע לכתוב לקובץ ו/או ל-Edit box ותעביר אותו ל-CDumpContext רגיל בתור היעד. מכוער, נכון, Welcome to MFC.
 
למעלה