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 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; } 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
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
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). 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
"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 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
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.
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.
> 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
> 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?
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
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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: JKop |
last post by:
What's the difference between them?
Take the following:
#include <iostream>
struct Blah
{
int k;
|
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
|
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;
|
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...
|
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,
|
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
{
|
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:
'...
|
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...
|
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...
|
by: subramanian100in |
last post by:
Suppose I have
#include <cstdlib>
#include <iostream>
using namespace std;
class Test
{
public:
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
| |