a simple question
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: a simple question

  1. #1
    Join Date
    Mar 2005
    Posts
    53

    a simple question

    Hi,

    What is the difference between
    Code:
    double a = 4.5;
    and
    Code:
    double a(4.5);
    Both gives the same output with
    Code:
    cout << a << endl;
    Thanks.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,433

    Re: a simple question

    Quote Originally Posted by manojg View Post
    Hi,

    What is the difference between
    Code:
    double a = 4.5;
    and
    Code:
    double a(4.5);
    Both gives the same output with
    Code:
    cout << a << endl;
    Thanks.
    There is no difference. In C++, even though "a" is a simple double, for consistency it is advantageous for the syntax to initialize this variable to be similar to object initialization.
    Code:
    #include <string>
    
    int main()
    {
       std::string s1("abc123");
       std::string s2 = "abc123";
    }
    Note the similarities between the code above using the std::string class, and your code using the double.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Mar 2005
    Posts
    53

    Re: a simple question

    Thanks.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,315

    Re: a simple question

    Note that there may be a difference if the type is a class type rather than double, i.e., the former would not be allowed if the corresponding constructor was declared as explicit.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center