Oddity with pointer initializer

S

Smeckler

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.
 
J

John Carson

Smeckler said:
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' ?


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

John Carson

Smeckler said:
OK, just to clarify:

MyClass* p = NULL;

is fine, whereas

MyClass* p(NULL);

gives "error C2059: syntax error : 'constant'" in VC6.

Both forms are fine in VC++ 7.
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.

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.
I wonder if I should be writing:

MyClass (*p)(NULL);

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

Karl Heinz Buchegger

Smeckler said:
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' ?

What is the exact error message?

So is the right way?

MyClass (*p)(NULL);

Or is gcc correct to allow the above?

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;
 
A

Alf P. Steinbach

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' ?

It works fine in VC7.

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

So is the right way?

MyClass (*p)(NULL);

That's unnecessarily complicated.


Or is gcc correct to allow the above?

Yep.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top