Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.lang.c++.moderated    |    Moderated discussion of C++ superhackery    |    33,346 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 31,385 of 33,346    |
|    Leigh Johnston to leo    |
|    Re: References confusion    |
|    17 Jul 11 17:10:46    |
      057c4195       From: leigh@i42.co.uk              On 17/07/2011 23:33, leo wrote:       > Hi,       >       > I have a problem here where I am not sure how to handle it. Firstly I       > am beginner. I have two constructors for a class and two reference       > variables declared but I can not use both references in one       > constructor. If cons1() is called I need ref1 to be used and if cons2       > is called I need ref2 to be used. The problem is in what should I       > reference ref1 to, when cons2 is called and similarly what ref2 should       > reference when cons1 is called. I am not sure how to initialize these       > references. It can't be NULL. I am not sure if its a good idea to       > point to some invalid implementation. Should it? Is that even an       > option? How is such a problem handled in c++?       >       >       > Class A       > cons1(Object1& a) : ref1(a) - here how should ref2 be handled?       > cons2(Object2& b) : ref2(b) - here what should ref1 reference?       >       > Object1& ref1       > Object2& ref2       >       > Appreciate any advice.              The simplest solution is to use pointers rather than references and initialize       the other pointer to 0.              /Leigh                     --        [ See http://www.gotw.ca/resources/clcm.htm for info about ]        [ comp.lang.c++.moderated. First time posters: Do this! ]              --- SoupGate-Win32 v1.05        * Origin: you cannot sedate... all the things you hate (1:229/2)    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca