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 32,599 of 33,346   
   red floyd to red floyd   
   Re: Strange convertion   
   19 Oct 12 22:55:07   
   
   From: no.spam.here@its.invalid   
      
   On 10/19/2012 3:36 PM, red floyd wrote:   
   > On 10/19/2012 1:04 PM, Vianney Lançon wrote:   
   >> Hello,   
   >>   
   >> while compiling our code we found that a wrong constructor was called.   
   >>   
   >> The code was something like that.   
   >>   
   >>   
   >> #include    
   >> struct Toto   
   >> {   
   >> Toto(unsigned short a[2]){ x=a[0]; y=a[1];}   
   >> unsigned short x, y;   
   >> };   
   >> void print(const Toto& toto)   
   >> {   
   >> std::cout<> }   
   >> int main()   
   >> {   
   >> print(false);  // <= convert false to Toto   
   >> return 0;   
   >> }   
   >>   
   >>   
   >> Because the constructor of Toto is not explicit it is normal that   
   >> unsigned short[4] can be converterd to Toto.   
   >>   
   >> But I do not understand by what mechanisme the bool value false can be   
   >> convert into unsigned short[4].   
   >>   
   >> Because it doesn't compile if i change the value from false to true it   
   >> 's probably because of some convertion from false to 0 literal.   
   >   
   > I believe it's because unsigned short[2] decays to unsigned short*.   
   > The chain then is false => 0 => (unsigned short*)0.   
      
   If you want an array only, pass a reference to it.   
      
   Toto(unsigned short (&a)[2]);   
      
   This will *ONLY* accept an array of 2 shorts.   
      
      
   --   
         [ 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