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,386 of 33,346    |
|    =?ISO-8859-1?Q?Daniel_Kr=FCgler?= to All    |
|    Re: References confusion    |
|    17 Jul 11 17:11:06    |
   
   057c4195   
   From: daniel.kruegler@googlemail.com   
      
   Am 18.07.2011 00:33, schrieb leo:   
   > 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.   
      
   You probably need to provide more details about what you are trying to do,   
   otherwise it will be hard to give reasonable advice. Also, please replace your   
   just indicated code by real C++ code, because otherwise it's hard to draw a   
   line between imagination    
   and reality.   
      
   >From what you write your class currently looks like this:   
      
   class Object1;   
   class Object2;   
      
   class A {   
    Object1& ref1;   
    Object2& ref2;   
   public:   
    A(Object1& a) : ref1(a) {}   
    A(Object2& b) : ref2(b) {}   
   };   
      
   which is not yet well-formed, of-course. Two immediate thoughts come into my   
   mind:   
      
   a) ref1 and ref2 are required, not optional. This means that you have to   
   initialize both in either case. This means that you have to provide some   
   default objects, e.g. you could have static data members. This could lead to   
   the following design:   
      
   class A {   
    Object1& ref1;   
    Object2& ref2;   
    static Object1 default1;   
    static Object2 default2;   
   public:   
    A(Object1& a) : ref1(a), ref2(default2) {}   
    A(Object2& b) : ref1(default1), ref2(b) {}   
   };   
      
   // One definition, typically provided in .cpp. Both   
   // types Object1 and Object2 must be complete types   
   // at this point:   
      
   class Object1 { /*...*/ };   
   class Object2 { /*...*/ };   
      
   Object1 A::default1;   
   Object2 A::default2;   
      
   b) ref1 and ref2 are optional, this means they can be pointers instead that   
   can be set to 0:   
      
   class A {   
    Object1* ptr1;   
    Object2* ptr2;   
   public:   
    A(Object1& a) : ptr1(&a), ptr2() {}   
    A(Object2& b) : ptr1(), ptr2(&b) {}   
   };   
      
   [Note: By writing ptr1() and ptr2() in the initializer-list, the pointer   
   values are value-initialized. This has the same effect as assigning NULL to   
   them].   
      
   HTH & Greetings from Bremen,   
      
   - Daniel Krügler   
      
      
   --   
    [ 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