473,385 Members | 1,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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

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

output: Hey!
Nov 17 '05 #1
15 1454
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
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
Nov 17 '05 #2
> 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.
Nov 17 '05 #3
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 :)
Nov 17 '05 #4
Gabest <ga****@freemail.hu> wrote:
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.

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

Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Nov 17 '05 #5
> 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!"?
Nov 17 '05 #6
Gabest <ga****@freemail.hu> wrote:
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!"?

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

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Nov 17 '05 #7

"Hendrik Schober" <Sp******@gmx.de> skrev i meddelandet
news:%2****************@TK2MSFTNGP11.phx.gbl...
Gabest <ga****@freemail.hu> wrote:
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!"?

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

Nov 17 '05 #8
>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
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
Nov 17 '05 #9
Bo Persson <bo*@gmb.dk> wrote:
[...]
This is yet another case of the redundant parenthesis in a declaration!
[...]
Bo Persson

Thanks. I didn't see it...

Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Nov 17 '05 #10
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
jb*******@yahoo.co.in

Nov 17 '05 #11
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;


Can you see any in the posted sample? :) That one compiles as is, only needs
a standard c header for printf.
Nov 17 '05 #12
> 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
Nov 17 '05 #13
>> 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.


X(qwerty);

is effectively:

X qwerty;

?

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
Nov 17 '05 #14

"Gabest" <ga****@freemail.hu> skrev i meddelandet
news:eh**************@tk2msftngp13.phx.gbl...
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?


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
Nov 17 '05 #15

"Bo Persson" <bo*@gmb.dk> skrev i meddelandet
news:O3**************@TK2MSFTNGP09.phx.gbl...

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


Nov 17 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Alex Sedow | last post by:
Anybody know how to create libboost_filesystem-vc71-s-1_31.lib? Please help me. 1. Download boost library. 2. Run boost\build\jam_src\build.bat. After that jam-files have been created in...
2
by: E.T. Grey | last post by:
#ifndef MKT_DATA_QUOTE_CHAIN_Header_ #define MKT_DATA_QUOTE_CHAIN_Header_ //#include "Quote.h" class QuoteChain/*: public Quote*/ { /*public: QuoteChain(); ~QuoteChain(); */
7
by: rn5a | last post by:
This is the second time I am asking this question in this newsgroup since I haven't got a solution or response from anyone in my previous post & I need to resolve this issue desperately. Sorry for...
2
by: BruceWho | last post by:
I downloaded boost1.35.0 and built it with following command: bjam --toolset=msvc-7.1 --variant=release --threading=multi -- link=shared --with-system stage and it failed to compile, error...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.