implicit constructor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rahul

    #1

    implicit constructor

    Hi Everyone,

    I know that implicit conversion is invoked for the following case,

    class A
    {
    public: A(int n)
    {
    ...
    }
    };


    A obj = 2;

    But is any temp objects created? Is the 2 used to create a temp
    object and then is the temp object assigned to the the actual object
    obj?

    Thanks in advance!!!

  • Victor Bazarov

    #2
    Re: implicit constructor

    Rahul wrote:
    Hi Everyone,
    >
    I know that implicit conversion is invoked for the following case,
    >
    class A
    {
    public: A(int n)
    {
    ...
    }
    };
    >
    >
    A obj = 2;
    >
    But is any temp objects created? Is the 2 used to create a temp
    object and then is the temp object assigned to the the actual object
    obj?
    The semantics are such that a temporary object is created. However,
    the Standard allows the compiler to forgo creation of a temporary in
    such case and construct 'obj' as if you wrote

    A obj(2);

    The copy-constructor in 'A' has to still be accessible, though.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • Rolf Magnus

      #3
      Re: implicit constructor

      Rahul wrote:
      Hi Everyone,
      >
      I know that implicit conversion is invoked for the following case,
      >
      class A
      {
      public: A(int n)
      {
      ...
      }
      };
      >
      >
      A obj = 2;
      >
      But is any temp objects created? Is the 2 used to create a temp
      object and then is the temp object assigned to the the actual object
      obj?
      It's not assigned, but rather copy-constructed. Apart from that, see
      Victor's answer.

      Comment

      Working...