Initialization of single-constructor class objects

Discussion in 'C++' started by Dave Theese, Aug 24, 2003.

  1. Dave Theese

    Dave Theese Guest

    Hello all,

    Consider a class foo that has a public, non-explicit constructor that can be
    called with one argument (either there's only one parameter or the second
    and subsequent parameters all have default values). For the sake of making
    the discussion concrete, let's say that argument is of type int. There are
    two syntaxes to define an object of this class:

    foo var(5);
    foo var = 5;

    Is there *any* semantic difference at all (no matter how small) between
    these two forms?

    Thanks,
    Dave
    Dave Theese, Aug 24, 2003
    #1
    1. Advertising

  2. "Dave Theese" <> wrote in message
    news:w9b2b.7899$QT5.7238@fed1read02...
    >
    > foo var(5);
    > foo var = 5;
    >
    > Is there *any* semantic difference at all (no matter how small) between
    > these two forms?


    Yes. The first version is "direct initialization," the second is "copy
    initialization." The first version creates a foo by calling the constructor
    that takes one int. The second version creates a temporary foo by calling
    the constructor that takes one int, and then creats var by passing the
    temporary foo to the foo copy constructor.

    Okay, maybe not. The C++ standard actually allows compilers to make the
    second version do the same thing as the first version. But there's no
    guarantee.

    Here's a good discussion:

    http://www.semantics.org/gotchas/gotcha34.pdf

    Regards,

    Russell Hanneken
    Russell Hanneken, Aug 24, 2003
    #2
    1. Advertising

  3. Dave Theese

    Dave Theese Guest

    Ahhh, OK. My thoughts were exactly along the lines that perhaps a temporary
    would be constructed; that's why I asked the question. In the case I tried,
    I declared a copy constructor private and did not define it to prevent this.
    Everything still compiled and ran fine. But I wasn't sure if that behavior
    could be counted on. It sounds as if it cannot.

    Along a similar vein, I've noticed that when I return an object from a
    function (by value, not by reference), there is no temporary constructed.
    Of course, there will be in the general case and this cannot be counted on
    either. I suppose these cases where temporaries would normally be expected
    but do not appear are compiler optimizations.

    Thanks man!

    "Russell Hanneken" <> wrote in message
    news:Rob2b.2056$...
    > "Dave Theese" <> wrote in message
    > news:w9b2b.7899$QT5.7238@fed1read02...
    > >
    > > foo var(5);
    > > foo var = 5;
    > >
    > > Is there *any* semantic difference at all (no matter how small) between
    > > these two forms?

    >
    > Yes. The first version is "direct initialization," the second is "copy
    > initialization." The first version creates a foo by calling the

    constructor
    > that takes one int. The second version creates a temporary foo by calling
    > the constructor that takes one int, and then creats var by passing the
    > temporary foo to the foo copy constructor.
    >
    > Okay, maybe not. The C++ standard actually allows compilers to make the
    > second version do the same thing as the first version. But there's no
    > guarantee.
    >
    > Here's a good discussion:
    >
    > http://www.semantics.org/gotchas/gotcha34.pdf
    >
    > Regards,
    >
    > Russell Hanneken
    >
    >
    >
    >
    Dave Theese, Aug 25, 2003
    #3
  4. "Dave Theese" <> wrote in message
    news:RAb2b.7905$QT5.7097@fed1read02...
    >
    > Along a similar vein, I've noticed that when I return an object from a
    > function (by value, not by reference), there is no temporary constructed.


    Yes, the C++ standard explicitly allows the compiler to not make a temporary
    in that case.

    > I suppose these cases where temporaries would normally be expected
    > but do not appear are compiler optimizations.


    The word "optimizations" might be misleading; the "optimized" code in this
    case could behave differently from the "unoptimized" code. The copy
    constructor might have side effects that never happen because the copy
    constructor doesn't get called.

    Here's the relevant passage from the standard:

    Whenever a temporary class object is copied using a copy
    constructor, and this object and the copy have the same cv-
    unqualified type, an implementation is permitted to treat the
    original and the copy as two different ways of referring to the
    same object and not perform a copy at all, even if the class copy
    constructor or destructor have side effects. For a function with
    a class return type, if the expression in the return statement is
    the name of a local object, and the cv-unqualified type of the
    local object is the same as the function return type, an
    implementation is permitted to omit creating the temporary object
    to hold the function return value, even if the class copy
    constructor or destructor has side effects. In these cases, the
    object is destroyed at the later of times when the original and
    the copy would have been destroyed without the
    optimization (section 12.8, paragraph 15).

    Regards,

    Russell Hanneken
    Russell Hanneken, Aug 25, 2003
    #4
  5. On Sun, 24 Aug 2003 18:26:40 -0700, mjm wrote:

    > That a constructor takes an integer argument does not mean that integers
    > can be assigned to the object constructed:
    >
    > class A {
    >
    > double* k;
    >
    > A(int d) : k(new double[d]) { }
    >
    > };
    >
    >
    > Now the statement
    >
    > A x(5);
    >
    > makes sense and allocate A::k as an array of five doubles. But the
    > assignement
    >
    > A x=5;
    >
    > makes no sense.


    It's not assignment. It uses the equal sign, but in this case, it's not
    the assignment operator. It's copy initialization, just like Russell said.

    You're right that it doesn't make much sense (from a human's point of
    view) in this circumstance, but it's still legal. In such a situation, you
    probably want to make the constructor `explicit'.

    Josh
    Josh Sebastian, Aug 25, 2003
    #5
  6. Dave Theese

    mjm Guest

    Suppose in my example we don't define operator =.
    Then what happens if the statement

    A x=5;

    is executed?
    mjm, Aug 25, 2003
    #6
  7. Dave Theese

    Sam Holden Guest

    On 25 Aug 2003 00:41:47 -0700, mjm <> wrote:
    > Suppose in my example we don't define operator =.
    > Then what happens if the statement
    >
    > A x=5;
    >
    > is executed?


    A constructor of A gets called. If no matching constructor exists then it
    doesn't get executed since it doesn't compile.

    --
    Sam Holden
    Sam Holden, Aug 25, 2003
    #7
  8. "mjm" <> wrote in message
    news:...
    > Suppose in my example we don't define operator =.
    > Then what happens if the statement
    >
    > A x=5;
    >
    > is executed?


    Russell Hanneken has already explained this in this thread

    A x=5;

    is more or less equivalent to

    A x(A(5));

    The point is that the '=' sign does not mean assignment in this context. In
    fact it couldn't mean assignment since assignment only happens when an
    object has already been constructed. In this case you are constructing x.

    john
    John Harrison, Aug 25, 2003
    #8
  9. Dave Theese

    mjm Guest

    Thanks,

    This is interesting and counter intuitive.
    I was aware of how it in the case of

    A x=y;

    where y is of type A.
    mjm, Aug 25, 2003
    #9
    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. mrstephengross
    Replies:
    5
    Views:
    584
    Larry I Smith
    May 18, 2005
  2. ali
    Replies:
    4
    Views:
    558
    David Harmon
    Mar 5, 2007
  3. Generic Usenet Account
    Replies:
    10
    Views:
    2,197
  4. balaji
    Replies:
    3
    Views:
    922
    Jens Thoms Toerring
    Aug 10, 2011
  5. j j
    Replies:
    4
    Views:
    678
    Alf P. Steinbach
    Jun 23, 2012
Loading...

Share This Page