Sign in to follow this  
draconar

my compiler got dumb (aka gnomes are running the computers)

Recommended Posts

draconar    130
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:
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?!

Share this post


Link to post
Share on other sites
draconar    130
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..

Share this post


Link to post
Share on other sites
Endurion    5411
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.

Share this post


Link to post
Share on other sites
Bregma    9214
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.

Share this post


Link to post
Share on other sites
draconar    130
Quote:
Original post by Bregma
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.


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. :(

Share this post


Link to post
Share on other sites
draconar    130
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Makaan    100
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

Share this post


Link to post
Share on other sites
DevFred    840
Quote:
Original post by Makaan
string q(); is a totally different story

Yes, because here you declare a function named q which takes no parameters and returns a string :)

I think you actually meant to write string q;

Share this post


Link to post
Share on other sites
Makaan    100
Quote:
Original post by DevFred
I think you actually meant to write string q;


Yea , well, i usually write that way to make sure i see a constructor call there.

Share this post


Link to post
Share on other sites
nobodynews    3126
Quote:
Original post by Makaan
Quote:
Original post by DevFred
I think you actually meant to write string q;


Yea , well, i usually write that way to make sure i see a constructor call there.
Except I'm pretty sure string g(); declares a function named g that returns a string and takes 0 parameters.

Share this post


Link to post
Share on other sites
draconar    130
Quote:
Original post by nobodynews
Quote:
Original post by Makaan
Quote:
Original post by DevFred
I think you actually meant to write string q;


Yea , well, i usually write that way to make sure i see a constructor call there.
Except I'm pretty sure string g(); declares a function named g that returns a string and takes 0 parameters.


you're right, nobodynews.

As Stroustrup puts in pgs. 322 and 323 from PPP


string s1; //default value: the empty string ""
string s1("Ike"); //String initialized to Ike
string s1 = string(); //default value: the empty string ""
string s2(); //function taking no argument returning a string


As there is no such examples for built-in types, I got confused. But he is refering to the '()', the notation for initializers.

Share this post


Link to post
Share on other sites
Vortez    2714
Why the hell would you want to use a construtor/destructor for basic compiler type like int or BYTE when all you have to do is use the '=' like this -> int i = 0; ??? I think it would just overcomplicate things for nothing, + it's easier to read this way imo.

Share this post


Link to post
Share on other sites
draconar    130
Quote:
Original post by Vortez
Why the hell would you want to use a construtor/destructor for basic compiler type like int or BYTE when all you have to do is use the '=' like this -> int i = 0; ??? I think it would just overcomplicate things for nothing, + it's easier to read this way imo.


ah, I would never use them this 'unusual' way. Made the post to understand how c++ treats built-in types as objects.

BTW (can't correctly recall) Java has a primitive type and an object type for int, double, etc. Which, IMHO, doesn't make any sense. If the language is object oriented, than things should be objects! As, by this post and Stroustrup book, it is in C++.

Share this post


Link to post
Share on other sites
bobofjoe    322
Quote:
Original post by Vortez
Why the hell would you want to use a construtor/destructor for basic compiler type like int or BYTE when all you have to do is use the '=' like this -> int i = 0; ??? I think it would just overcomplicate things for nothing, + it's easier to read this way imo.


It lets you have a uniform treatment of types in a template. For instance, this (useless) template works on anything that is default constructible; including primitive types.


template<typename T>
T default_construct()
{
return T();
}

assert(default_construct<int>() == 0);
assert(default_construct<std::string>() == "");

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this