int i;
cout << i << endl;
the result on screen will be 0.
Ok, in my notebook (VC++ 2008 express) this works alright. But not in my desktop! which is running VC++ 2008 express too.
In my desktop, the value of i on screen is a random garbage. what is this?!
the only difference I could spot was that the notebook got a "using namespace System" while the desktop don't. Actually, the "using namespace System" doesn't even works in the Desktop. The compiler says:
1>.\apagar.cpp(9) : error C2871: 'System' : a namespace with this name does not exist
what is this? different versions of the compiler?!
my compiler got dumb (aka gnomes are running the computers)
Hello all,
I'm working through Stroustrup's newest book and here and there I'm discovering some novelty.
As an example, I learned that even "primitive" types have constructors in C++. Like an int is in reality an int obj and they have automatic initialization:
Quote:Original post by Badgerr
You are printing out an uninitialized variable, which has undefined results.
Try int i = 0;
nops. c++ has a default constructor for built-in types. When I write just "int i;", would be the same as writing
int i = int();
which initializes correctly to zero, because it called the default constructor explicitly.
somehow one compiler is doing it correctly (calling the default constructor) and the other don't..
The line "Using namespace System" looks like you created a managed C++ project (note the managed). Things behave quite different there.
Plain vanilla C++ does NOT initialize primitives.
Plain vanilla C++ does NOT initialize primitives.
Quote:Original post by draconar
what is this? different versions of the compiler?!
Different languages. Bjarne writes about C++, a language standardized by the ISO. You are using a language called C++/CLR invented by Microsoft on your notebook. They are vaguely related.
You are incorrect in your reading of Stroustrup, by the way.
int i;
is not equivalent to int i = int(0);
according to the language definition.
Quote:Original post by BregmaQuote:Original post by draconar
what is this? different versions of the compiler?!
Different languages. Bjarne writes about C++, a language standardized by the ISO. You are using a language called C++/CLR invented by Microsoft on your notebook. They are vaguely related.
You are incorrect in your reading of Stroustrup, by the way.is not equivalent toint i;
according to the language definition.int i = int(0);
yeah. that makes sense.
So as for the ANSI C++ goes,
int i; <-- is unitialized variable
int i = int(); <-- explicity calls the default constructor, which initializes to zero.
actually in page 323 (from PPP) he says
For built-in types, such as int and double, the default constructor notation means 0, so int() is a complicated way of saying 0, and double() a long-winded way of saying 0.0
but I extrapolated his examples using string,
string s1; //default value: the empty string ""
which is not a built-in type.
BTW, the passage I quote refers to the (), the default constructor notation. Not to automatic initialization of built-in types. ouch. :(
Quote:Original post by Endurion
The line "Using namespace System" looks like you created a managed C++ project (note the managed). Things behave quite different there.
Plain vanilla C++ does NOT initialize primitives.
Endurion,
you nailed it.
I created a managed c++ project in VC++ 2008. How can I be sure to always create an ANSI C++ project?
After selecting "New Project" from the file menu, you should end up with a tree view in a pane off to the left with a bunch of project types. One of the nodes should be Visual C++ and underneath that you should see several more project types (ATL, CLR, General, MFC...).
A CLR Project is a C++/CLI (Managed) Project. You want the project type "Win32".
Choose a "Win32 Console Application" (I think) and when the Application Wizard pops up, click "Next" and under "Additional options" choose "Empty Project"
Hope that helps.
A CLR Project is a C++/CLI (Managed) Project. You want the project type "Win32".
Choose a "Win32 Console Application" (I think) and when the Application Wizard pops up, click "Next" and under "Additional options" choose "Empty Project"
Hope that helps.
I would also suggest in the future to explicitly initialise your variables and not rely on the compilier to do it for you.
As far as i know Gcc or Msvc dosen't initialize pod types with any value;
int i; will probably not be 0;Don't trust the book at these minor detailes and manually write i=0 if you want i to be 0;
string q(); is a totally different story, it involes memory allocation so for safety it must be "" be default
int i; will probably not be 0;Don't trust the book at these minor detailes and manually write i=0 if you want i to be 0;
string q(); is a totally different story, it involes memory allocation so for safety it must be "" be default
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement