assignment vs initialization

This is a discussion on assignment vs initialization within the C++ Programming forums, part of the General Programming Boards category; We have a coding guideline that says "Always use () instead of =, unless not possible (code optimization). For example: ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    330

    assignment vs initialization

    We have a coding guideline that says

    "Always use () instead of =, unless not possible (code optimization).
    For example: int x(3); instead of int x = 3;"

    so we are not allowed to type X x = 2;

    The argument being that it is faster to use the X x(2) form because then only the constructor is called and with X x = 2 the constructor is called and then the copy constructor.

    Im convinced this is just not true but I can't persuade them and I cant find the paragraph in the standard where it talks about explicit constructor calls and the form with '='.
    Anyone knows where to find this or am I wrong in thinking that the forms dont differ?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >The argument being that it is faster to use the X x(2) form because then only the
    >constructor is called and with X x = 2 the constructor is called and then the copy constructor.
    Ha! Unless the constructor is declared as explicit (in which case the second form is illegal), the two are identical. You can write a test program to strengthen your case:
    Code:
    #include <iostream>
    
    class X {
    public:
        X(int) { std::cout<<"Default\n"; }
        X(const X&) { std::cout<<"Copy\n"; }
    };
    
    int main()
    {
        X a = 1;
        X b(1);
    }
    >Anyone knows where to find this or am I wrong in thinking that the forms dont differ?
    The specific clauses can be found in section 12.3.1: Conversion by constructor. Specifically this one:
    Code:
    A constructor declared without the function-specifier explicit that can be called with a 
    single parameter specifies a conversion from the type of its first parameter to the type 
    of its class. Such a constructor is called a converting constructor. [ Example:
    
    class X {
        // ...
    public:
        X(int);
        X(const char*, int =0);
    };
    
    void f(X arg)
    {
        X a = 1; // a = X(1)
        X b = "Jessie"; // b = X("Jessie",0)
        a = 2; // a = X(2)
        f(3); // f(X(3))
    }
    
    —end example ]
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Since every modern compiler should compile them to the identical machine code, I think it is only matter of code readability, and for me the second form seems better (at least for the fundamental types).

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    #include <iostream>
    
    class X {
    public:
        X(int) { std::cout<<"Default\n"; }
        X(const X&) { std::cout<<"Copy\n"; }
    };
    
    int main()
    {
        X a = 1;
        X b(1);
    }
    Pretty much the only difference here should be that the first version requires an available copy constructor. (You'll get an error if you declare the copy constructor private.)

    But that doesn't mean the copy constructor will be invoked. Constructor calls can be elided in certain cases, just that the code must be valid as if they weren't elided.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Regarding const array initialization
    By codegeru in forum C++ Programming
    Replies: 7
    Last Post: 07-19-2009, 11:55 AM
  2. Initialization and assignment
    By rajesh1978 in forum C++ Programming
    Replies: 7
    Last Post: 03-18-2009, 09:13 AM
  3. Assignment vs. Initialization
    By arrgh in forum C Programming
    Replies: 6
    Last Post: 05-06-2008, 04:08 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  5. Replies: 1
    Last Post: 10-27-2006, 02:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21