Oddity with pointer initializer

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

    Oddity with pointer initializer

    I can't find any references to this in google's archive, but I'm sure it
    must be an old chestnut...

    The declaration

    MyClass* p(NULL);

    is fine under gcc, but not VC6. I suspect VC6 thinks this is a declaration
    of a pointer to a function taking an argument of type 'NULL' ?

    So is the right way?

    MyClass (*p)(NULL);

    Or is gcc correct to allow the above?

    Cheers all.


  • John Carson

    #2
    Re: Oddity with pointer initializer

    "Smeckler" <smNOecklers@ho tSPAMmail.com> wrote in message
    news:bg2voq$hkn $1$8302bc10@new s.demon.co.uk[color=blue]
    > I can't find any references to this in google's archive, but I'm sure
    > it must be an old chestnut...
    >
    > The declaration
    >
    > MyClass* p(NULL);
    >
    > is fine under gcc, but not VC6. I suspect VC6 thinks this is a
    > declaration of a pointer to a function taking an argument of type
    > 'NULL' ?[/color]


    NULL is not a type, so your code cannot be a function declaration. VC 7
    compiles it without a problem.


    --
    John Carson
    1. To reply to email address, remove donald
    2. Don't reply to email address (post here instead)

    Comment

    • John Carson

      #3
      Re: Oddity with pointer initializer

      "Smeckler" <smNOecklers@ho tSPAMmail.com> wrote in message
      news:bg358t$o2i $1$8300dec7@new s.demon.co.uk[color=blue][color=green][color=darkred]
      > > > The declaration
      > > >
      > > > MyClass* p(NULL);
      > > >
      > > > is fine under gcc, but not VC6. I suspect VC6 thinks this is a
      > > > declaration of a pointer to a function taking an argument of type
      > > > 'NULL' ?[/color]
      > >
      > >
      > > NULL is not a type, so your code cannot be a function declaration.
      > > VC 7 compiles it without a problem.
      > >[/color]
      >
      > OK, just to clarify:
      >
      > MyClass* p = NULL;
      >
      > is fine, whereas
      >
      > MyClass* p(NULL);
      >
      > gives "error C2059: syntax error : 'constant'" in VC6.[/color]

      Both forms are fine in VC++ 7.
      [color=blue]
      > The only thing I can think is that VC is parsing it as a "ptr to
      > function" declaration and thinks that NULL is supposed to be the type
      > of the argument to the function.[/color]

      I am not sure of the exact history here, but the traditional way to
      initialise a built in type is with

      BuiltInType variable = value;

      The traditional way to initialise a class object is with

      Class object(value);

      (assuming a single argument constructor). In order to permit a more
      consistent syntax, at some point it became possible to initialise built-in
      types (including pointers) using the syntax for initialising a class object.
      My guess is that VC++6 simply pre-dates that change or at least Microsoft
      hadn't got around to fully implementing it at that stage.
      [color=blue]
      > I wonder if I should be writing:
      >
      > MyClass (*p)(NULL);
      >[/color]

      This also works with VC++7 but only because the brackets have no effect in
      this case.


      --
      John Carson
      1. To reply to email address, remove donald
      2. Don't reply to email address (post here instead)

      Comment

      • Karl Heinz Buchegger

        #4
        Re: Oddity with pointer initializer



        Smeckler wrote:[color=blue]
        >
        > I can't find any references to this in google's archive, but I'm sure it
        > must be an old chestnut...
        >
        > The declaration
        >
        > MyClass* p(NULL);
        >
        > is fine under gcc, but not VC6. I suspect VC6 thinks this is a declaration
        > of a pointer to a function taking an argument of type 'NULL' ?
        >[/color]

        What is the exact error message?

        [color=blue]
        > So is the right way?
        >
        > MyClass (*p)(NULL);
        >
        > Or is gcc correct to allow the above?[/color]

        The above is fine, since NULL is no data type but either:
        * an undeclared identifier
        * the literal constant 0 or an equivalent expression,
        due to inclusion of some header which defines the
        macro NULL in that way.

        and none of those 2 make sense in a function declaration, so it
        can't be a function declaration but it must be a variable
        declaration with an initialization.

        You could write:

        MyClass* p = NULL;

        --
        Karl Heinz Buchegger
        kbuchegg@gascad .at

        Comment

        • Alf P. Steinbach

          #5
          Re: Oddity with pointer initializer

          On Mon, 28 Jul 2003 12:00:59 +0100, "Smeckler" <smNOecklers@ho tSPAMmail.com> wrote:
          [color=blue]
          >I can't find any references to this in google's archive, but I'm sure it
          >must be an old chestnut...
          >
          >The declaration
          >
          >MyClass* p(NULL);
          >
          >is fine under gcc, but not VC6. I suspect VC6 thinks this is a declaration
          >of a pointer to a function taking an argument of type 'NULL' ?[/color]

          It works fine in VC7.

          Have you included a definition of NULL, e.g. via [cstddef]?

          [color=blue]
          >So is the right way?
          >
          >MyClass (*p)(NULL);[/color]

          That's unnecessarily complicated.


          [color=blue]
          >Or is gcc correct to allow the above?[/color]

          Yep.

          Comment

          Working...