Variable Initialization: MSVC vs. world

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thomas Matthews

    #1

    Variable Initialization: MSVC vs. world

    Hi,

    When assigning variables at initialization, is the following
    allowed:
    class Field;

    int main(void)
    {
    Field * p_field(NULL);
    return EXIT_SUCCESS;
    }

    I've switch to using MS Visual C++ 6.0 and it is giving me
    errors for the assignment. However, Borland 5.2 and GNU
    g++ 3.4.4 do not give any errors.

    My belief is that the above is correct syntax.

    Unfortunately, I'm have to change all of these and
    similar instances to:
    Field * p_field = NULL;
    to get the code to compiler under MSVC++ 6.0

    --
    Thomas Matthews

    C++ newsgroup welcome message:

    C++ Faq: http://www.parashift.com/c++-faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.l earn.c-c++ faq:

    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    http://www.sgi.com/tech/stl -- Standard Template Library
  • Phlip

    #2
    Re: Variable Initialization: MSVC vs. world

    Thomas Matthews wrote:
    Field * p_field(NULL);
    I've switch to using MS Visual C++ 60 and it is giving
    me errors for the assignment.
    VC++ >6 permits that correct notation.

    I suspect there are those who will promote constructor-notation for all
    initializations . However, I can't think of a rationale besides "it's just
    obviously better!"

    --
    Phlip
    http://www.greencheese.us/ZeekLand <-- NOT a blog!!!


    Comment

    • Jerry Coffin

      #3
      Re: Variable Initialization: MSVC vs. world

      In article <YcARg.7723$Ij. 2977@newssvr14. news.prodigy.co m>,
      phlipcpp@yahoo. com says...

      [ ... ]
      I suspect there are those who will promote constructor-notation for all
      initializations . However, I can't think of a rationale besides "it's just
      obviously better!"
      The obvious reasoning here is that constructor notation instructs the
      compiler to construct the object with the value directly. Notation like:

      whatever x=something;

      really instructs the compiler to create a temporary object initialized
      with 'something', then use whatever's copy constructor to create x, then
      destroy the temporary. For built-in types, this is rarely a problem, but
      for other types that take longer to create, copy and/or destroy, this is
      substantially slower. Likewise, for extremely large objects, this wastes
      memory creating two objects where only one is really needed.

      --
      Later,
      Jerry.

      The universe is a figment of its own imagination.

      Comment

      • loufoque

        #4
        Re: Variable Initialization: MSVC vs. world

        Phlip wrote :
        I suspect there are those who will promote constructor-notation for all
        initializations . However, I can't think of a rationale besides "it's just
        obviously better!"
        The constructor notation can cause problems related to function declaration.

        Comment

        • Victor Bazarov

          #5
          Re: Variable Initialization: MSVC vs. world

          Jerry Coffin wrote:
          In article <YcARg.7723$Ij. 2977@newssvr14. news.prodigy.co m>,
          phlipcpp@yahoo. com says...
          >
          [ ... ]
          >
          >I suspect there are those who will promote constructor-notation for
          >all initializations . However, I can't think of a rationale besides
          >"it's just obviously better!"
          >
          The obvious reasoning here is that constructor notation instructs the
          compiler to construct the object with the value directly. Notation
          like:
          >
          whatever x=something;
          >
          really instructs the compiler to create a temporary object initialized
          with 'something', then use whatever's copy constructor to create x,
          then destroy the temporary. For built-in types, this is rarely a
          problem, but for other types that take longer to create, copy and/or
          destroy, this is substantially slower. Likewise, for extremely large
          objects, this wastes memory creating two objects where only one is
          really needed.
          The reality is a bit better but somewhat convoluted, I am afraid.
          The compiler is explicitly allowed to forgo creation of a temporary
          in such case, even if the copy c-tor has side effects (see 12.8/15).
          However, the copy c-tor has to be accessible *as if* copying does take
          place.

          V
          --
          Please remove capital 'A's when replying by e-mail
          I do not respond to top-posted replies, please don't ask


          Comment

          • loufoque

            #6
            Re: Variable Initialization: MSVC vs. world

            Thomas Matthews wrote :
            I've switch to using MS Visual C++ 6.0
            Then switch again to a C++ compiler.

            Comment

            • Bart

              #7
              Re: Variable Initialization: MSVC vs. world

              Thomas Matthews wrote:
              Hi,
              >
              When assigning variables at initialization, is the following
              allowed:
              class Field;
              >
              int main(void)
              {
              Field * p_field(NULL);
              return EXIT_SUCCESS;
              }
              >
              I've switch to using MS Visual C++ 6.0 and it is giving me
              errors for the assignment.
              Well, that's what you get for using an 8-year-old compiler. If you're
              going to use MSVC at least use the most recent version (which is a lot
              more standard-compliant BTW).

              Regards,
              Bart.

              Comment

              • Jeff Faust

                #8
                Re: Variable Initialization: MSVC vs. world


                loufoque wrote:
                Thomas Matthews wrote :
                >
                I've switch to using MS Visual C++ 6.0
                >
                Then switch again to a C++ compiler.
                It compiles on VC++ 8, which is much much closer to being a C++
                compiler.

                Jeff

                Comment

                Working...