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 31,493 of 33,346   
   Martin B. to Thiago Adams   
   Re: How to move immutable objects?   
   27 Sep 11 10:23:27   
   
   e3299e98   
   From: 0xCDCDCDCD@gmx.at   
      
   On 26.09.2011 20:27, Thiago Adams wrote:   
   > I was playing with a string class which I want to represent   
   > an immutable string object.   
   >   
   > class string {   
   >    const char* const m_psz;   
   > public:   
   >    string(const char* psz) : m_psz(_strdup(psz)) {}   
   >    ~string() { free((void*)m_psz); }   
   >    //..only const methods...   
   > };   
   >   
   > When I wrote the move constructor the compiler complained   
   > about the const data member (m_psz).   
   >   
   > string(string&&  s) : m_psz(s.m_psz)   
   > {   
   >      s.m_psz = nullptr; //error l-value specifies const object   
   > }   
   >   
   > Then I realized that is not be possible to move from const data   
   > members.   
   >   
   > What would you do in this case?   
   >   
   > 1) Do not use move constructor?   
   > 2) Remove the const data member keeping the const member functions?   
   > 3) ?   
   >   
   >   
      
   What about making m_psz a shared_ptr? It adds some overhead, sure.   
   But being able to share the member string buffer between multiple   
   immutable objects obviates the need for the move ctor, as the copy ctor   
   can just make the new object point to the same buffer as the copied-from   
   object.   
      
   cheers,   
   Martin   
      
   --   
   Stop Software Patents   
   http://petition.stopsoftwarepatents.eu/841006602158/   
   http://www.ffii.org/   
      
      
         [ 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