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,934 of 33,346    |
|    SG to Bobby    |
|    Re: member function returns by-value    |
|    14 Mar 13 07:13:45    |
   
   5c4ca604   
   From: s.gesemann@googlemail.com   
      
   On Mar 14, 12:25 pm, Bobby wrote:   
   > [...] type X [...] an "incomplete type" [...]   
   >   
   > But why then is it possible (both with g++ and VC) to   
   > define a function returning a X?   
      
   [...]   
      
   > class X {   
   > public:   
   > X() {}   
   > X foo(); // <-- why does this compile?   
   > private:   
   > // X x_; // x_ has incomplete type   
   > std::string s_;   
   > };   
      
   First of all, this is a DECLARATION, not a DEFINITION. If you just   
   declare a function, the return type and parameter types don't need to   
   be complete. But they need to be complete where you want to invoke or   
   DEFINE it -- usually.   
      
   However, this does not apply to member function definitions that are   
   inside a class definition with respect to the class type. You can   
   think of   
      
    class X {   
    public:   
    X(int v) : member(v) {}   
    X foo() {return X(member+1);};   
    private:   
    int member;   
    };   
      
   just being a shortcut syntax for   
      
    class X {   
    public:   
    X(int v);   
    X foo();   
    private:   
    int member;   
    };   
      
    // now X is complete :-)   
      
    inline X::X(int v) : member(v) {}   
      
    inline X X::foo() {return X(member+1);};   
      
   With this in mind, you see why referencing 'member' in X::X and X::foo   
   and creating an X object in X::foo actually works.   
      
   Cheers!   
   SG   
      
      
   --   
    [ 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