Sign in to follow this  
digitalfreak

question about comiler generated default constructor.

Recommended Posts

digitalfreak    164
what's the difference between an explicitly defined empty constructor(no arg and empty body) and a comiler generated default constructor? from section 10.4.2 (p243) of the c++ programming language(special edition), it seems the latter one doesn't initialize members of build-in types while the former one does (from my experiment). is that correct.

Share this post


Link to post
Share on other sites
Enigma    1410
I don't think there's any difference at all.
#include <iostream>

class DefaultConstructor
{

public:

int i;

};

class EmptyConstructor
{

public:

EmptyConstructor()
{
}

int i;
};

void fillStack()
{
int someVar = 0xF8E9DACB;
}

void makeDefaultConstructedObject()
{
// make sure the stack is filled with messy values
fillStack();
DefaultConstructor dc;
std::cout << dc.i << '\n';
}

void makeEmptyConstructedObject()
{
// make sure the stack is filled with messy values
fillStack();
EmptyConstructor ec;
std::cout << ec.i << '\n';
}

int main()
{
makeDefaultConstructedObject();
makeEmptyConstructedObject();
}


Enigma

Share this post


Link to post
Share on other sites
chollida1    532
Did you test this in releas or debug. When you launch your app from the debugger( in my case vs.net) the debugger will init vars to 0, even in release.

If you launch it outside the debugger then it won't init the vars in release

Try using logging to see what happens in release outside of the debugger.

Cheers
Chris

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