473,395 Members | 1,675 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,395 software developers and data experts.

Value initialization. . .

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
Jul 22 '05 #1
14 1447
> #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();
const 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;
}

Jul 22 '05 #2
const NonCopyable &blah = *new NonCopyable();

.. . . Why?
Perhaps you're mistaken with:
NonCopyable const &blah = NonCopyable();
the "const" modifier would be necessary there alright. (I think)

....but anyway you'd have an invalid reference. (I think)
-JKop

Jul 22 '05 #3
Gernot Frisch wrote:
#include <iostream>

class NonCopyable
{
private:

NonCopyable(NonCopyable const &);
Add
NonCopyable& operator=(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();

const 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;
}


Victor
Jul 22 '05 #4
JKop wrote:

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 issue is NO longer POD vs. non-POD in standard C++. The issue is
whether there is a user defined default constructor.

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();


What bullshit compllication are you talking about.
Class object();
is a function decaration.
Class object;
is a default constructed object (for non POD's).
Jul 22 '05 #5
Problem A) A temporary


Which all compilers elide.

If the Standard enforced that, I wouldn't have any problems whatsoever.

Alas, the Standard does not.
-JKop
Jul 22 '05 #6
"Gernot Frisch" <Me@Privacy.net> wrote:
int main()
{
NonCopyable &blah = *new NonCopyable();


const NonCopyable &blah = *new NonCopyable();

This is a terrible idea, as you have to remember to do
"delete &blah" later. This will confuse almost anybody
else looking at the code, because it is usual to refer
to new'd objects by a pointer. Especially if the 'new' and
'delete' are not nearby in the code.

If you really feel the need to use . notation instead of
->, go:
std::auto_ptr<NonCopyable> ptr(new NonCopyable());
NonCopyable const &blah = *ptr;
....
// no delete required
Jul 22 '05 #7
This is a terrible idea, as you have to remember to do
"delete &blah" later.

I have to remember to consume water every three days. . . ain't dead yet.

This will confuse almost anybody
else looking at the code, because it is usual to refer
to new'd objects by a pointer. Especially if the 'new' and
'delete' are not nearby in the code.

It's perfectly legal C++, it one is confused by it, then they don't know
C++.

If you really feel the need to use . notation instead of
->, go:
std::auto_ptr<NonCopyable> ptr(new NonCopyable());
NonCopyable const &blah = *ptr;
...
// no delete required


Or... I could use *my* method, which is approx. 5 billion times more
efficient.
-JKop
Jul 22 '05 #8
JKop wrote:
This is a terrible idea, as you have to remember to do
"delete &blah" later.
I have to remember to consume water every three days. . . ain't dead yet.

This will confuse almost anybody
else looking at the code, because it is usual to refer
to new'd objects by a pointer. Especially if the 'new' and
'delete' are not nearby in the code.

It's perfectly legal C++,


It is. However, it's very ugly, too. The only thing that could prevent you
from writing ugly code is experience in reading ugly code.
it one is confused by it, then they don't know C++.


People will understand it, but will need longer, because there is no
sensible reason to do what you are doing. And it will always take people
longer to understand unreasonable things.
If you really feel the need to use . notation instead of
->, go:
std::auto_ptr<NonCopyable> ptr(new NonCopyable());
NonCopyable const &blah = *ptr;
...
// no delete required


Or... I could use *my* method, which is approx. 5 billion times more
efficient.


I doubt you ever tested what the difference is.

Jul 22 '05 #9
Ron Natalie wrote:
JKop wrote:

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 issue is NO longer POD vs. non-POD in standard C++. The issue is
whether there is a user defined default constructor.

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();


What bullshit compllication are you talking about.
Class object();
is a function decaration.
Class object;
is a default constructed object (for non POD's).


And

Class object = {0};

is a zero initialized POD object, which seems to be what the OP is searching
for.
Jul 22 '05 #10
> It is. However, it's very ugly, too. The only thing that could
prevent you
from writing ugly code is experience in reading ugly code.


Agree. I juist wanted to solve the problem, not the style. But, I'd
never write that code. I don't even like the overloading of -> if it
can be avoided.
Or... I could use *my* method, which is approx. 5 billion times
more
efficient.


I doubt you ever tested what the difference is.


I think auto_ptr is quite fast. There's not much difference here. But
reading this code is as difficult as the original method (for me). I
don'tīlike auto_ptr's. new. delete. It's that simple. I can't think of
anything why I would like to have garbage collectors (that's why C# is
suppoest to be so much "easier").
I'm goint OT now, sorry.
-Gernot
Jul 22 '05 #11
> The issue is NO longer POD vs. non-POD in standard C++. The issue
is
whether there is a user defined default constructor.


What is a POD?
Jul 22 '05 #12
On Thu, 07 Oct 2004 17:09:00 GMT, JKop <NU**@NULL.NULL> wrote:
Problem A) A temporary


Which all compilers elide.

If the Standard enforced that, I wouldn't have any problems whatsoever.

Alas, the Standard does not.


For classes with private copy constructors, why don't you just write:

MyClass myObject; //default constructor called.

Tom
Jul 22 '05 #13
Gernot Frisch wrote:
The issue is NO longer POD vs. non-POD in standard C++. The issue
is whether there is a user defined default constructor.


What is a POD?


You should have a look at the FAQ to this newsgroup, especially question
[26.7] What is a "POD type"? The FAQ is posted here at a regular basis and
also available e.g. from http://www.parashift.com/c++-faq-lite/
Jul 22 '05 #14
Gernot Frisch wrote:
The issue is NO longer POD vs. non-POD in standard C++. The issue
is
whether there is a user defined default constructor.


What is a POD?


http://www.parashift.com/c++-faq-lit....html#faq-26.7


Brian Rodenborn
Jul 22 '05 #15

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

Similar topics

10
by: JKop | last post by:
What's the difference between them? Take the following: #include <iostream> struct Blah { int k;
13
by: simondex | last post by:
Hi, Everyone! Does anyone know how to initialize an int array with a non-zero number? Thank You Very Much. Truly Yours, Simon Dexter
7
by: Jamie Julius | last post by:
Consider the following struct: struct TestStruct { public int a, b, c; public TestStruct(int a, int b, int c) { this.a = a;
4
by: Ben R. | last post by:
I'm curious about the differeng behavior of the "new" keyword when dealing with value versus object types. If I'm correct, when I do: dim x as integer There's no need for "new" to be called...
10
by: utab | last post by:
Dear all, Can somebody direct me to some resources on the subject or explain the details in brief? I checked the FAQ but could not find or maybe missed. Regards,
5
by: wkaras | last post by:
I've compiled this code: const int x0 = 10; const int x1 = 20; const int x2 = 30; int x = { x2, x0, x1 }; struct Y {
4
by: shapper | last post by:
Hello, I am creating a class where I have various properties. How to I set a default property value in case the property is not defined by the user. For example, I have the property: '...
23
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
4
by: subramanian100in | last post by:
Suppose I have #include <cstdlib> #include <iostream> using namespace std; class Test { public:
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.