Click here to Skip to main content
15,910,872 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: I thought I understood polymorphism Pin
Alex Griffing3-Jul-01 12:16
Alex Griffing3-Jul-01 12:16 
GeneralRe: I thought I understood polymorphism Pin
Ben Burnett3-Jul-01 13:40
Ben Burnett3-Jul-01 13:40 
GeneralRe: I thought I understood polymorphism Pin
Alex Griffing3-Jul-01 13:57
Alex Griffing3-Jul-01 13:57 
GeneralRe: I thought I understood polymorphism Pin
Stan Shannon3-Jul-01 14:40
Stan Shannon3-Jul-01 14:40 
GeneralRe: I thought I understood polymorphism Pin
Alex Griffing3-Jul-01 16:33
Alex Griffing3-Jul-01 16:33 
GeneralRe: I thought I understood polymorphism Pin
Stan Shannon3-Jul-01 17:38
Stan Shannon3-Jul-01 17:38 
GeneralRe: I thought I understood polymorphism Pin
3-Jul-01 21:12
suss3-Jul-01 21:12 
GeneralRe: I thought I understood polymorphism Pin
Malcolm McMahon3-Jul-01 23:24
Malcolm McMahon3-Jul-01 23:24 
I've been caught out by this one in the past. What happens is that the object, although allocated enough space for a fooplus is first initialised as a foo object, then the foo constructor is called. At that point the foo virtual method table is in effect. Only once the foo constructor exits does the fooplus intialisation occur and the VMT switches to the one containing fooplus::bar. This behaviour is logical, fooplus::bar might refer to variables in fooplus that the foo constructor doesn't know to initialise.

How you get arround it depends on details of what you're doing that we don't know. If you're really getting into polymorphic programming you'll often find the best solution is static factory routines which give you a uniform access to constructors e.g.

<br />
<br />
class foo {<br />
protected:<br />
    foo(...) {<br />
     };<br />
public:<br />
    static foo *factory( ... );<br />
    virtual void bar();<br />
    };<br />
<br />
class fooplus : public foo {<br />
protected:<br />
     fooplus( ... ) : foo(plus) {<br />
      }<br />
<br />
public:<br />
    static foo *factory( ... );<br />
    virtual void bar;<br />
  ..<br />
<br />
   };<br />
<br />
foo *foo::facory( ... ) {<br />
  foo *f = new foo( ...);<br />
  f->bar();<br />
  return f;<br />
  }<br />
<br />
foo *fooplus::factory ( ...) {<br />
  fooplus *f = new fooplus ( ...);<br />
  f->bar;<br />
  return f;<br />
  }<br />
<br />


Because the factory methods are static and have the same signature you can store pointers to them in a table of variations on foo.
QuestionWho can give me a help???? Pin
3-Jul-01 11:26
suss3-Jul-01 11:26 
Questionmultiple active windows in MDI? Pin
Jake Palmer3-Jul-01 11:15
Jake Palmer3-Jul-01 11:15 
AnswerRe: multiple active windows in MDI? Pin
3-Jul-01 12:19
suss3-Jul-01 12:19 
GeneralRe: multiple active windows in MDI? Pin
Jake Palmer3-Jul-01 14:31
Jake Palmer3-Jul-01 14:31 
GeneralRe: multiple active windows in MDI? Pin
Ben Burnett3-Jul-01 18:45
Ben Burnett3-Jul-01 18:45 
AnswerRe: multiple active windows in MDI? Pin
#realJSOP4-Jul-01 1:06
professional#realJSOP4-Jul-01 1:06 
AnswerRe: multiple active windows in MDI? Pin
Jose Cruz4-Jul-01 11:35
Jose Cruz4-Jul-01 11:35 
GeneralGetCurrentDirectory Pin
3-Jul-01 10:45
suss3-Jul-01 10:45 
GeneralRe: GetCurrentDirectory Pin
Ghazi H. Wadi3-Jul-01 10:50
Ghazi H. Wadi3-Jul-01 10:50 
GeneralWord and WM_VSCROLL Pin
BobMatura3-Jul-01 10:08
BobMatura3-Jul-01 10:08 
Questionhow to include source code Pin
uma3-Jul-01 9:26
uma3-Jul-01 9:26 
AnswerRe: how to include source code Pin
Jonathon Lockett3-Jul-01 12:54
Jonathon Lockett3-Jul-01 12:54 
GeneralSoundBuffer problems. Pin
Daniel Selberg3-Jul-01 8:27
Daniel Selberg3-Jul-01 8:27 
Generalquestion about linking to a static library Pin
3-Jul-01 8:25
suss3-Jul-01 8:25 
GeneralRe: question about linking to a static library Pin
Stan Shannon3-Jul-01 8:52
Stan Shannon3-Jul-01 8:52 
GeneralRe: question about linking to a static library Pin
markkuk3-Jul-01 21:07
markkuk3-Jul-01 21:07 
GeneralAre you a proffesional you should see this!! Pin
Rickard Andersson203-Jul-01 8:22
Rickard Andersson203-Jul-01 8:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.