Thread: double constructor & complex initialisation

  1. #1
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812

    double constructor & complex initialisation

    Hi there,

    In C++, if I do this:

    Code:
    double a;
    Is 'a' guaranteed to be 0.0 according to the standard?

    I ask because, I am interested in doing this:

    Code:
    std::complex<double> b = 5.0;
    If the default constructor of double does not guarantee zero, then presumably the value of the imaginary component of 'b' could be anything?
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I don't believe C++ initializes variable. You can see this if you declare a variable, define it, and print it to the screen. You'll see gibberish and seemingly random data. It's just the binary that was previously occupying those bytes.

    I am studying for the Java Certification however, so I could be wrong...

  3. #3
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    >I don't believe C++ initializes variable.

    Mmmm. That what I thought. However, if this is the case it makes the default constructors for the complex class dangerous.

    It means I should be doing this to be safe:

    Code:
    complex<double> b(5.0, 0.0);
    and this

    Code:
    if ( b == complex<double>(5.0, 0.0) ) ...
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Yes, double does default to 0.0, and if you construct a complex from a single double, the imaginary part will be 0.0:
    Code:
    std::complex<double> b(5.0);
    assert(b == std::complex<double>(5.0, 0.0));
    However,
    Code:
    double a;
    is still an unintialized variable if it is not global (in which case it is 0.0).

  5. #5
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Thanks for the reply. I'm a little confused.

    The complex constructor looks like so:

    complex (const T& re_arg = T(), const T& im_arg = T());

    If I were, to do this:

    Code:
    complex<double> b = 5.0;
    presumably, the imaginary component is equal to whatever the default value for a double constructor assigns.

    Are you suggesting:

    Code:
    double a;
    does not initialise 'a' to zero, but:

    Code:
    double a();
    would?
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  6. #6
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Follow up. I guess this explains things:

    (from the C++ documentation)
    complex<T>& operator=(const T& v);
    Assigns v to the real part of itself, setting the imaginary part to 0.

    Scrub my previous post & thanks for replying.
    Last edited by Davros; 09-10-2004 at 07:22 PM.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> I guess this explains things
    No. This
    >> std::complex<double> b = 5.0;
    will call a constructor, not the overloaded operator.

    Don't confuse
    >> double a;
    with
    >> double a = double();

    One is unititialized and the other is zero initialized per the standard.

    gg

  8. #8
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Quote Originally Posted by Codeplug
    >> I guess this explains things
    No. This
    >> std::complex<double> b = 5.0;
    will call a constructor, not the overloaded operator.
    Although in this case the complex constructor exhibits the same behavior, so if you refer to the constructor instead of the assignment operator then it explains things better.

  9. #9
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    >Don't confuse
    >> double a;
    >with
    >> double a = double();
    >One is unititialized and the other is zero initialized per the standard.

    That was something I didn't know & was very helpful.

    Cheers to you both for the help.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 02:54 PM
  2. Help with multi function progam
    By WackoWolf in forum C Programming
    Replies: 22
    Last Post: 10-13-2005, 02:56 AM
  3. Please HELP!!
    By traz in forum C++ Programming
    Replies: 4
    Last Post: 04-14-2003, 09:20 PM
  4. Problem from texbook
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-26-2002, 04:55 AM
  5. what's the difference?
    By iluvmyafboys in forum C++ Programming
    Replies: 13
    Last Post: 02-28-2002, 09:25 PM