Click here to Skip to main content
15,909,741 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: returning by reference and polymorphism Pin
Iain Clarke, Warrior Programmer10-Sep-09 9:48
Iain Clarke, Warrior Programmer10-Sep-09 9:48 
Let's do some actual code...

class B
{
public:
   virtual B & myFunc () { return *this; }
   virtual void SayMyName () { printf ("B"); }
};

class C : public B
{
public:
   virtual B &myFunc () { return *this; }
   virtual void SayMyName () { printf ("C"); }
}

main ()
{
   C c;
   B b = c.myFunc;
   b.SayMyName (); // prints B
}


OK, pretty artificial, and I'm missing out on stuff.

It's pretty plain to me that a temporary B would be constructed, using it's copy constructor. Then if the compiler is not too clever, that temporary B would be copied to b.

If you want a polymorphic return, use pointers.

class B
{
public:
   virtual B *myFunc () { return this; }
   virtual void SayMyName () { printf ("B"); }
};

class C : public B
{
public:
   virtual B *myFunc () { return this; }
   virtual void SayMyName () { printf ("B"); }
}

main ()
{
   C c;
   B *b = c.myFunc;
   b->SayMyName (); // prints C
}


Give that a go, I hope it helps.

Iain.

I have now moved to Sweden for love (awwww).
If you're in Scandinavia and want an MVP on the payroll (or happy with a remote worker), or need cotract work done, give me a job! http://cv.imcsoft.co.uk/[^]

AnswerRe: returning by reference and polymorphism Pin
Iain Clarke, Warrior Programmer10-Sep-09 9:49
Iain Clarke, Warrior Programmer10-Sep-09 9:49 
AnswerRe: returning by reference and polymorphism Pin
CPallini10-Sep-09 9:59
mveCPallini10-Sep-09 9:59 
GeneralRe: returning by reference and polymorphism Pin
paolosh10-Sep-09 11:35
paolosh10-Sep-09 11:35 
Questiontic tac toe Pin
sam_200910-Sep-09 5:18
sam_200910-Sep-09 5:18 
QuestionRe: tic tac toe Pin
David Crow10-Sep-09 5:20
David Crow10-Sep-09 5:20 
AnswerRe: tic tac toe Pin
PIEBALDconsult10-Sep-09 5:58
mvePIEBALDconsult10-Sep-09 5:58 
AnswerRe: tic tac toe Pin
Keith Barrow10-Sep-09 5:26
professionalKeith Barrow10-Sep-09 5:26 
GeneralRe: tic tac toe Pin
Selvam R10-Sep-09 7:29
professionalSelvam R10-Sep-09 7:29 
AnswerRe: tic tac toe Pin
Richard MacCutchan10-Sep-09 7:34
mveRichard MacCutchan10-Sep-09 7:34 
AnswerWrong forum. Pin
CPallini10-Sep-09 8:31
mveCPallini10-Sep-09 8:31 
GeneralRe: Wrong forum. Pin
EliottA10-Sep-09 8:33
EliottA10-Sep-09 8:33 
AnswerRe: tic tac toe Pin
jinjiashan10-Sep-09 16:49
jinjiashan10-Sep-09 16:49 
QuestionCreating a Time Provider in C++ Pin
bijumanjeri10-Sep-09 5:11
bijumanjeri10-Sep-09 5:11 
AnswerRe: Creating a Time Provider in C++ Pin
David Crow10-Sep-09 5:18
David Crow10-Sep-09 5:18 
GeneralRe: Creating a Time Provider in C++ Pin
bijumanjeri10-Sep-09 5:30
bijumanjeri10-Sep-09 5:30 
GeneralRe: Creating a Time Provider in C++ Pin
David Crow10-Sep-09 5:42
David Crow10-Sep-09 5:42 
GeneralRe: Creating a Time Provider in C++ Pin
bijumanjeri10-Sep-09 5:50
bijumanjeri10-Sep-09 5:50 
AnswerRe: Creating a Time Provider in C++ Pin
Randor 10-Sep-09 6:02
professional Randor 10-Sep-09 6:02 
GeneralRe: Creating a Time Provider in C++ Pin
bijumanjeri10-Sep-09 18:31
bijumanjeri10-Sep-09 18:31 
QuestionUsing void* to keep track of an external object. Pin
RedSonja10-Sep-09 4:15
RedSonja10-Sep-09 4:15 
AnswerRe: Using void* to keep track of an external object. Pin
Richard MacCutchan10-Sep-09 7:43
mveRichard MacCutchan10-Sep-09 7:43 
GeneralRe: Using void* to keep track of an external object. Pin
RedSonja10-Sep-09 20:50
RedSonja10-Sep-09 20:50 
GeneralRe: Using void* to keep track of an external object. Pin
Richard MacCutchan11-Sep-09 1:19
mveRichard MacCutchan11-Sep-09 1:19 
GeneralRe: Using void* to keep track of an external object. Pin
RedSonja17-Sep-09 20:41
RedSonja17-Sep-09 20:41 

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.