home bbs files messages ]

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 33,252 of 33,346   
   Francis Glassborow to Martin Bonner   
   Re: Dereferencing pointer as argument to   
   30 Oct 13 12:27:14   
   
   From: francis.glassborow@btinternet.com   
      
   On 30/10/2013 16:29, Martin Bonner wrote:   
   > On Wednesday, October 30, 2013 12:00:02 AM UTC, Robert Simpson wrote:   
   >> My question is, if I have a function taking an argument by   
   >> reference, is it bad practice to call that function with a   
   >> dereferenced pointer?   
   >   
   > No, provided you know (possibly because you have already checked)   
   > that the pointer is not null.   
   >   
   >   
   >> void myfunc( Widget& w );   
   >> std::unique_ptr< Widget > mywidget;   
   >   
   >    mywidget = new Widget; // Need to set mywidget.   
   >   
   >> myfunc( *mywidget.get() );   
   >   
   >> I have written this in my code, but I believe that   
   >> there is probably a better solution.   
   >   
   > "Better" in what sense?   
   >   
   > If your pointer *can* be null, then you can't dereference   
   > it to pass it to the function.  You need a genuine object   
   > to bind the reference to.   
   >   
   >   
      
   But you could provide an overload of the function that accepts a   
   pointer, checks that it it points to an actual object and then   
   forwards to the original function. Failure could be handled through   
   throwing an exception.   
      
   Francis   
      
      
      
   --   
         [ 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