hm, is this ok to compile without any error? (vc71)

  • Thread starter Thread starter Gabest
  • Start date Start date
G

Gabest

class X
{
public:
X() {_tprintf(_T("Hey!\n"));}
};

int _tmain(int argc, _TCHAR* argv[])
{
X(qwerty);
return 0;
}

output: Hey!
 
Yes, it compiles OK with VC7.1, and it's also ok with the online
Comeau compiler.

What were you trying to point out?

Dave
 
Yes, it compiles OK with VC7.1, and it's also ok with the online
Comeau compiler.

What were you trying to point out?

Just strange that class X takes no parameter in its constructor and I can
still pass one: "qwerty", which was defined nowhere.
 
One more note about why I asked if it was ok. This behavior from the
compiler confused me yesterday when I accidentally renamed one of my
functions to an already existing class name. Then it didn't compile of
course. But when I was renaming the function and the refereces back what
they were before, I forgot to do it at one place, and that called the
default constructor of the class instead of my old function. It took me a
few minutes to realize what was happening :)
 
Gabest said:
Just strange that class X takes no parameter in its constructor and I can
still pass one: "qwerty", which was defined nowhere.


I think it's just taking this
X(qwerty);
part as a function declaration.
That's a rather common annoyance
in C++.

Schobi

--
(e-mail address removed) is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
 
I think it's just taking this
X(qwerty);
part as a function declaration.
That's a rather common annoyance
in C++.

You mean function X with the default int return type? What would be the type
of qwerty then, also int? Why does my sample code still print out "Hey!"?
 
Gabest said:
You mean function X with the default int return type? What would be the type
of qwerty then, also int? Why does my sample code still print out "Hey!"?


You're right.
I just played with this and it is
indeed calling the default ctor
three times:

class X
{
public:
X()
{
}
};

int main(int /*argc*/, char* /*argv*/[])
{
X(blah);
X();
X x;
return 0;
}

(Note: I removed all headers to make
sure there's no weird macro involved.)

And, yes, Comeau (4.3.3) accepts it as well:

--8<----8<----8<----8<----8<----8<----8<----8<--

C:\Develop>test.exe
Ooops!
Ooops!
Ooops!

C:\Develop>type test.cpp
#include <stdio.h>

class X
{
public:
X()
{
printf("Ooops!\n");
}
};

int main(int /*argc*/, char* /*argv*/[])
{
X(blah);
X();
X x;
return 0;
}

C:\Develop>como --A -otest.exe test.cpp
Comeau C/C++ 4.3.3 (Aug 10 2003 15:39:53) for _MS_WINDOWS_x86_Beta8
Copyright 1988-2003 Comeau Computing. All rights reserved.
MODE:strict errors C++


C:\Develop>test.exe
Ooops!
Ooops!
Ooops!

C:\Develop>

-->8---->8---->8---->8---->8---->8---->8---->8--

Puzzled,

Schobi

--
(e-mail address removed) is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
 
Hendrik Schober said:
Gabest said:
You mean function X with the default int return type? What would be the type
of qwerty then, also int? Why does my sample code still print out
"Hey!"?


You're right.
I just played with this and it is
indeed calling the default ctor
three times:

class X
{
public:
X()
{
}
};

int main(int /*argc*/, char* /*argv*/[])
{
X(blah);
X();
X x;
return 0;
}

But....

This is yet another case of the redundant parenthesis in a declaration!

X(blah);

is equivalent to

X blah;

which is a declaration of a local variable blah of type X.


Bo Persson
 
Just strange that class X takes no parameter in its constructor and I can
still pass one: "qwerty", which was defined nowhere.

Haven't you actually just got an instance of X named qwerty?

X qwerty;

Dave
 
Bo Persson said:
[...]
This is yet another case of the redundant parenthesis in a declaration!
[...]
Bo Persson


Thanks. I didn't see it...

Schobi

--
(e-mail address removed) is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
 
Great!!
Thanks for the information I have got from this thread.. It solved one
unsolved problem in my project...

Saved me a lot of time...

Jigar Mehta
(e-mail address removed)
 
Haven't you actually just got an instance of X named qwerty?

X qwerty;

Can you see any in the posted sample? :) That one compiles as is, only needs
a standard c header for printf.
 
This is yet another case of the redundant parenthesis in a declaration!
X(blah);

is equivalent to

X blah;

which is a declaration of a local variable blah of type X.

Oh, and is this legal or a bug? Can I also write (X) blah, X()blah, ()X
blah, (X blah) then, just for fun? :P
 
Haven't you actually just got an instance of X named qwerty?
Can you see any in the posted sample? :) That one compiles as is, only needs
a standard c header for printf.

X(qwerty);

is effectively:

X qwerty;

?

Dave
 
Gabest said:
Oh, and is this legal or a bug?

No, this is legal (but *very* confusing).
Can I also write (X) blah, X()blah, ()X
blah, (X blah) then, just for fun? :P

No, its no fun at all. :-)


The "thing" being declared can be put inside a pair of parenthesis, because
sometimes it is needed ...

To declare a pointer to a function, you actually need to write

return_type (*Fptr)(param_type);

to show that the star goes with Fptr and not with the return type. If you
write it like

return_type * FPtr(param_type)

it's suddenly a function returning a pointer, not a pointer to a function
....

So, to make it "simple" you are just always allowed to put the name you
declare inside a pair of parenthesis. I guess it was just to difficult to
specify exactly when it could be needed!

Now we can write our declarations above as:

return_type (*Fptr)(param_type); // pointer to function returning a value
and
return_type* (Fptr)(param_type); // function returning a pointer to a
value


Unfortunately this rule makes this ok, and meaning the same:

type variable;
type (variable);
type(variable);

but the last one looks terribly much like these

class_name(initial_value);
function_name(formal_parameter);


The language rules simply say that if you can't tell, it is to be seen as a
declaration.


Bo Persson
 
The language rules simply say that if you can't tell, it is to be seen as a
declaration.

No, that wasn't strong enough!

If it can, by any stretch of the imagination, be seen as a declaration, it
must be considered a declaration. Even if it then doesn't compile!




Bo Persson
 
Back
Top