Click here to Skip to main content
15,885,782 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: std::wstring cannot convert from char[3] to class std::basic_string................. Pin
Stuart Dootson10-Sep-09 11:05
professionalStuart Dootson10-Sep-09 11:05 
Questionfwrite for unicode big endian Pin
Rakesh510-Sep-09 8:17
Rakesh510-Sep-09 8:17 
AnswerRe: fwrite for unicode big endian Pin
Selvam R10-Sep-09 8:53
professionalSelvam R10-Sep-09 8:53 
GeneralRe: fwrite for unicode big endian Pin
Rakesh510-Sep-09 9:08
Rakesh510-Sep-09 9:08 
GeneralRe: fwrite for unicode big endian Pin
Selvam R10-Sep-09 9:34
professionalSelvam R10-Sep-09 9:34 
Questionreturning by reference and polymorphism Pin
paolosh10-Sep-09 8:11
paolosh10-Sep-09 8:11 
AnswerRe: returning by reference and polymorphism Pin
paolosh10-Sep-09 9:46
paolosh10-Sep-09 9:46 
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 

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.