Re: Self Assignment check in Copy ctor

From: Risto Lankinen (rlankine_at_hotmail.com)
Date: 02/06/05

  • Next message: Peter Koch Larsen: "Re: Can basic_string<>::c_str() throw exceptions?"
    Date: 5 Feb 2005 23:37:10 -0800
    
    

    > AlgoMan wrote:
    > > Do you need to provide a self assignment check in a copy ctor
    > > (like you provide in an assignment operator).

    Artie Gold <artiegold@austin.rr.com> wrote in message news:<36ktrvF50haq3U1@individual.net>...
    > Two words: What `self'?

    Well, you know, the... the... the 'self':

     - - -

    struct C
    {
       C( const C &r )
       {
          if( this == &r )
          {
             cout << "Hello myself!" << endl;
          }
       }
    };

    int main()
    {
       C c(c);
    }

     - - -

    (Go ahead, try it out if you don't believe it works!)

    > AlgoMan wrote:
    > > If not, why not?

    Because the risk is very very small that somebody would
    accidentally copy-initialize with reference to self. It
    can be done, but the only way to do it is deliberately.

     - Risto -


  • Next message: Peter Koch Larsen: "Re: Can basic_string<>::c_str() throw exceptions?"

    Relevant Pages

    • Re: C++ Compiler behavior regerding struct constructors
      ... C++ guarantees zero initialization for static variables of primitive type, ... MyStruct m_MyStruct = MyStruct; ... > struct MyStruct ... > MyClass::MyClass//The default ctor for the class does not init the ...
      (microsoft.public.dotnet.languages.vc)
    • Calling a struct constructor in a class constructor body
      ... I need to call the struct ctor in the class default ctor body see below: ... struct MyStruct ...
      (microsoft.public.dotnet.languages.vc)
    • Re: defined in constructor
      ... "JSmith" wrote in message ... >> So this line only appears inside the ctor and the implementation of the ... >> is only in the cpp file? ... it should be global and please know that the struct is not defind ...
      (microsoft.public.vc.language)
    • C++ Compiler behavior regerding struct constructors
      ... I just notice some strange behaviors on the MS C++ compiler regarding struct ... struct MyStruct ... class MyClass ... MyClass::MyClass//The default ctor for the class does not init the struct ...
      (microsoft.public.dotnet.languages.vc)
    • Re: Self Assignment check in Copy ctor
      ... > Do you need to provide a self assignment check in a copy ctor ... > (like you provide in an assignment operator). ... "Do you smell something?" ...
      (comp.lang.cpp)