Is this copy initialization or direct initialization ?

Discussion in 'C++' started by subramanian100in@yahoo.com, India, Dec 30, 2008.

  1. , India

    , India Guest

    Suppose 'Test' is a class.

    Suppose I create an instance 'obj' of the 'Test' class:

    Test obj;

    Now, suppose I write

    Test another = obj;

    My question: is this last line called 'direct -initialization' or
    'copy-initialization' ?

    Kindly clarify.

    Thanks
    V.Subramanian
    , India, Dec 30, 2008
    #1
    1. Advertising

  2. , India

    Guest

    On Dec 30, 9:27 am, ", India"
    <> wrote:
    > Suppose 'Test' is a class.
    >
    > Suppose I create an instance 'obj' of the 'Test' class:
    >
    > Test obj;
    >
    > Now, suppose I write
    >
    > Test another = obj;
    >
    > My question: is this last line called 'direct -initialization' or
    > 'copy-initialization' ?
    >
    > Kindly clarify.
    >
    > Thanks
    > V.Subramanian


    Hi

    AFAIK, there isn't "direct -initialization" terminology in standard C+
    +. I think the term "copy-initialization"
    isn't standard too. What is clear is, in the following statements:
    Test obj;
    Test another = obj;
    first an object (obj) is created using default constructor, then
    another object (another) is created using
    copy constructor. The second one is called "initialization", because
    another is initialized using obj.

    Cheers,
    Saeed Amrollahi
    , Dec 30, 2008
    #2
    1. Advertising

  3. , India

    Guest

    On Dec 30, 9:27 am, ", India"
    <> wrote:
    > Suppose 'Test' is a class.
    >
    > Suppose I create an instance 'obj' of the 'Test' class:
    >
    > Test obj;
    >
    > Now, suppose I write
    >
    > Test another = obj;
    >
    > My question: is this last line called 'direct -initialization' or
    > 'copy-initialization' ?
    >
    > Kindly clarify.
    >
    > Thanks
    > V.Subramanian


    Hi

    I am sorry for some inaccuracy. My answer is not incorrect in general,
    but I read the
    C++ standard draft ISO/IEC JTC 1/SC 22 N 4411 and I found the
    following statements:

    The initialization that occurs in the form
    T x = a;
    as well as in argument passing, function return, throwing an
    exception, handling an exception,
    and aggregate member initialization is called copy-initialization.
    The initialization that occurs in in the forms
    T x(a);
    T x{a};
    as well as in new expressions, static_cast expressions, functional
    notation type conversions, and base and
    member initializers is called direct-initialization.
    So I think the statement:
    Test another = obj;
    should be copy-initialization.

    Cheers,
    Saeed Amrollahi
    , Dec 30, 2008
    #3
  4. , India

    prashanta

    Joined:
    Dec 30, 2008
    Messages:
    2
    Location:
    Bangalore
    Test another = obj; //copy-initialization
    Test another(obj); //copy-initialization
    another = obj; //direct -initialization
    prashanta, Dec 30, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. , India
    Replies:
    3
    Views:
    302
    Neelesh Bodas
    Jul 3, 2007
  2. , India
    Replies:
    2
    Views:
    1,092
    terminator
    Nov 23, 2007
  3. Replies:
    5
    Views:
    1,959
    Arne Mertz
    Mar 26, 2009
  4. , India
    Replies:
    5
    Views:
    395
    James Kanze
    Aug 21, 2009
  5. Taras_96
    Replies:
    3
    Views:
    574
    Michael Tsang
    Oct 30, 2009
Loading...

Share This Page