J
JKop
The following code demonstrates that when you do:
AnyNonPOD &blah = *new AnyNonPOD();
that the object's member variables *don't* get zero initialized.
I've been trying to find a way to create a non-const object of a non-POD
class and have all its member variables initialized to zero... the only
catch is that this non-POD will be non-copyable(ie. private copy
constructor). Anyway, the following code prints the following on my system:
I'm non-const!
Is it zero initialized: false
Here's the code:
#include <iostream>
class NonCopyable
{
private:
NonCopyable(NonCopyable const &);
public:
NonCopyable() {}
//Note that there's no initializer list to set
//things to zero.
unsigned int a;
unsigned char b;
void* p_c[9999999];
};
void Func(NonCopyable &)
{
std::cout << "I'm non-const!\n";
}
void Func(NonCopyable const &)
{
std::cout << "I'm const!\n";
}
int main()
{
NonCopyable &blah = *new NonCopyable();
Func(blah); //Making sure it's not a function declaration!
bool is_zero_initialized = true;
if ( !blah.a && !blah.b )
{
for(unsigned i = 0; i < 9999999; ++i)
{
if ( blah.p_c )
{
is_zero_initialized = false;
break;
}
}
}
else is_zero_initialized = false;
std::cout << "Is it zero initialized: " << ( is_zero_initialized ?
"true" : "false" );
delete &blah;
}
Overall, I'm trying to get around one of the top "bullshit complications" in
C++, in that you can't do:
Class object();
instead having to do:
Class object = Class();
which creates 2 problems.
Problem A) A temporary
Problem B) Copy constructor must be public
If only there were some clarity, as in:
Function Declaration: extern Class object();
Object Definition: class Class object();
and that when you did:
Class object();
or
Class object(void);
that it's a function declaration (which is how it is now).
-JKop
AnyNonPOD &blah = *new AnyNonPOD();
that the object's member variables *don't* get zero initialized.
I've been trying to find a way to create a non-const object of a non-POD
class and have all its member variables initialized to zero... the only
catch is that this non-POD will be non-copyable(ie. private copy
constructor). Anyway, the following code prints the following on my system:
I'm non-const!
Is it zero initialized: false
Here's the code:
#include <iostream>
class NonCopyable
{
private:
NonCopyable(NonCopyable const &);
public:
NonCopyable() {}
//Note that there's no initializer list to set
//things to zero.
unsigned int a;
unsigned char b;
void* p_c[9999999];
};
void Func(NonCopyable &)
{
std::cout << "I'm non-const!\n";
}
void Func(NonCopyable const &)
{
std::cout << "I'm const!\n";
}
int main()
{
NonCopyable &blah = *new NonCopyable();
Func(blah); //Making sure it's not a function declaration!
bool is_zero_initialized = true;
if ( !blah.a && !blah.b )
{
for(unsigned i = 0; i < 9999999; ++i)
{
if ( blah.p_c )
{
is_zero_initialized = false;
break;
}
}
}
else is_zero_initialized = false;
std::cout << "Is it zero initialized: " << ( is_zero_initialized ?
"true" : "false" );
delete &blah;
}
Overall, I'm trying to get around one of the top "bullshit complications" in
C++, in that you can't do:
Class object();
instead having to do:
Class object = Class();
which creates 2 problems.
Problem A) A temporary
Problem B) Copy constructor must be public
If only there were some clarity, as in:
Function Declaration: extern Class object();
Object Definition: class Class object();
and that when you did:
Class object();
or
Class object(void);
that it's a function declaration (which is how it is now).
-JKop