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

C / C++ / MFC

 
GeneralRe: Returning a Reference Pin
Stefan_Lang27-Jan-11 1:57
Stefan_Lang27-Jan-11 1:57 
GeneralRe: Returning a Reference Pin
TheGreatAndPowerfulOz27-Jan-11 8:23
TheGreatAndPowerfulOz27-Jan-11 8:23 
GeneralRe: Returning a Reference Pin
Niklas L26-Jan-11 23:20
Niklas L26-Jan-11 23:20 
GeneralRe: Returning a Reference Pin
Stefan_Lang26-Jan-11 23:57
Stefan_Lang26-Jan-11 23:57 
GeneralRe: Returning a Reference Pin
Niklas L27-Jan-11 1:07
Niklas L27-Jan-11 1:07 
GeneralRe: Returning a Reference Pin
Stefan_Lang27-Jan-11 1:20
Stefan_Lang27-Jan-11 1:20 
GeneralRe: Returning a Reference Pin
TheGreatAndPowerfulOz27-Jan-11 8:02
TheGreatAndPowerfulOz27-Jan-11 8:02 
AnswerRe: Returning a Reference [modified] Pin
Stefan_Lang26-Jan-11 23:12
Stefan_Lang26-Jan-11 23:12 
get() = argh; calls bclass::operator= which doesn't exist*. Even if it did exist, val is not a member of bclass and thus couldn't be copied anyway. If you define the return type of get() as dclass&, and in addtion to that define the assignment operator for this class, then it might work.

P.S.: to my knowledge assignment operators can't be made virtual, but you might be able to get this to work with a trick like this:
class base {
   virtual void assign(const base& other)=0; //<- helper function
public:
   base& operator=(const base& other) { assign(other); return *this; }
};
class derived : public base {
   virtual void assign(const base& other) {
      const derived* pOther = &(dynamic_cast<const derived&>(other));
      if (pOther != 0) { 
         val = pOther->val;
      }
   }
public:
   derived() : val(5) {}
   int val;
};
derived myglobalderived; // val is initialized to 5
derived& get() { return myglobalderived; }
void test() {
   derived mylocalderived;
   mylocalderived.val = 3;
   get() = mylocalderived;
   std::cout << "get() = " << get().val;
}


*P.P.S: while bclass::operator= hasn't been defined explicitely, the compiler might create one automatically (but it wouldn't make any difference as explained above). I am not sure in this case however, since assignment operators can't be virtual, and bclass is an abstract class. A member-wise copy will not make any sense, as you can't create instances for an abstract class, and derived classes will overwrite, not override the assignment operator.

modified on Thursday, January 27, 2011 5:58 AM

QuestionAny fast and efficient compress/decompress code? Pin
includeh1026-Jan-11 6:53
includeh1026-Jan-11 6:53 
AnswerRe: Any fast and efficient compress/decompress code? Pin
Anthony Mushrow26-Jan-11 7:41
professionalAnthony Mushrow26-Jan-11 7:41 
AnswerRe: Any fast and efficient compress/decompress code? Pin
Rolf Kristensen26-Jan-11 8:45
Rolf Kristensen26-Jan-11 8:45 
QuestionCapturing stdout from an external program Pin
piul26-Jan-11 3:50
piul26-Jan-11 3:50 
AnswerRe: Capturing stdout from an external program Pin
David Crow26-Jan-11 3:55
David Crow26-Jan-11 3:55 
GeneralRe: Capturing stdout from an external program Pin
piul26-Jan-11 4:43
piul26-Jan-11 4:43 
GeneralRe: Capturing stdout from an external program Pin
David Crow26-Jan-11 5:40
David Crow26-Jan-11 5:40 
QuestionHow to get a CBitmap from a LPDIRECTDRAWSURFACE7 [Solved] Pin
kagelind26-Jan-11 3:01
kagelind26-Jan-11 3:01 
AnswerRe: How to get a CBitmap from a LPDIRECTDRAWSURFACE7 Pin
Code-o-mat26-Jan-11 7:54
Code-o-mat26-Jan-11 7:54 
GeneralRe: How to get a CBitmap from a LPDIRECTDRAWSURFACE7 Pin
kagelind26-Jan-11 23:35
kagelind26-Jan-11 23:35 
GeneralRe: How to get a CBitmap from a LPDIRECTDRAWSURFACE7 Pin
Code-o-mat26-Jan-11 23:41
Code-o-mat26-Jan-11 23:41 
GeneralRe: How to get a CBitmap from a LPDIRECTDRAWSURFACE7 Pin
Code-o-mat26-Jan-11 23:44
Code-o-mat26-Jan-11 23:44 
GeneralRe: How to get a CBitmap from a LPDIRECTDRAWSURFACE7 Pin
kagelind27-Jan-11 2:28
kagelind27-Jan-11 2:28 
GeneralRe: How to get a CBitmap from a LPDIRECTDRAWSURFACE7 Pin
Code-o-mat27-Jan-11 2:36
Code-o-mat27-Jan-11 2:36 
GeneralRe: How to get a CBitmap from a LPDIRECTDRAWSURFACE7 Pin
kagelind27-Jan-11 4:08
kagelind27-Jan-11 4:08 
GeneralRe: How to get a CBitmap from a LPDIRECTDRAWSURFACE7 Pin
Code-o-mat27-Jan-11 4:11
Code-o-mat27-Jan-11 4:11 
QuestionIs there simple open source compiler code? Pin
yu-jian26-Jan-11 0:03
yu-jian26-Jan-11 0:03 

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.