If class members (attributes) are not explicitly initialized in a
constructor, are they zero-initialized OR are their contents undefined
when the object is constructed? Is this specified in the langauge
standard?
My guess is: they are undefined.
Thanks, Bob 6 1233
Bob Evans wrote: If class members (attributes) are not explicitly initialized in a constructor, are they zero-initialized OR are their contents undefined when the object is constructed? Is this specified in the langauge standard?
Yes.
My guess is: they are undefined.
The Standard says that the behaviour is to default-initialise the members
that are not mentioned in the initialisation list IFF those entities are
base classes or non-static members of a class (non-POD) type. If the
omitted member is of a POD type ("or array thereof"), it is left
uninitialised.
Example:
struct A {
int a;
std::string s;
A() {}
};
A a;
here 'a.a' has indeterminate value, whereas 'a.s' is an empty string.
V
Bob Evans wrote: If class members (attributes) are not explicitly initialized in a constructor, are they zero-initialized OR are their contents undefined when the object is constructed?
The answer depends on the following details:
1) type of the member (POD, aggregate, non-POD)
2) certain properties of the constructor of the enclosing class
(explicitly declared or not)
3) intializer used for the object of the enclosing class
Is this specified in the langauge standard?
Yes.
My guess is: they are undefined.
Not enough information. Would you please provide more concrete details?
--
Best regards,
Andrey Tarasevich
Bob Evans wrote: If class members (attributes) are not explicitly initialized in a constructor, are they zero-initialized OR are their contents undefined when the object is constructed? Is this specified in the langauge standard?
My guess is: they are undefined.
If the member is of non-POD type, it is default initialized.
If the class type is of POD type, and the storage duration
is static, it's zero-initialized, if it's auto, it's undefined,
if it's allocated dynamically, it depends on the form of the call
to new.
Yes it is rediculously inconsistent If the member is of non-POD type, it is default initialized. If the class type is of POD type, and the storage duration is static, it's zero-initialized, if it's auto, it's undefined, if it's allocated dynamically, it depends on the form of the call to new.
Yes it is rediculously inconsistent.
It's strange: When you read the above, it seems so complicated... but
after time you just get used to it!
I'll just stick to my good ol' "ValueInitialized" template for the time
being!
-JKop
Thanks to all respondents. It is sufficiently confusing that I shall
always initialize all my non-static members in my constructors. This
sounds like a "best practice" to me anyway. In some cases it may make
for a long initialization list.
Bob.
Ron Natalie <ro*@sensor.com> wrote in message news:<41***********************@news.newshosting.c om>... Bob Evans wrote: If class members (attributes) are not explicitly initialized in a constructor, are they zero-initialized OR are their contents undefined when the object is constructed? Is this specified in the langauge standard?
My guess is: they are undefined.
If the member is of non-POD type, it is default initialized. If the class type is of POD type, and the storage duration is static, it's zero-initialized, if it's auto, it's undefined, if it's allocated dynamically, it depends on the form of the call to new.
Yes it is rediculously inconsistent
"Bob Evans" <bt**********@yahoo.com> wrote... Thanks to all respondents. It is sufficiently confusing that I shall always initialize all my non-static members in my constructors. This sounds like a "best practice" to me anyway. In some cases it may make for a long initialization list.
It's not always possible, just so you know. For example, there is no way
to initialise arrays or other aggregates.
Bob.
Ron Natalie <ro*@sensor.com> wrote in message news:<41***********************@news.newshosting.c om>... Bob Evans wrote: > [.. I guess you didn't really mean to quote it so I'll remove it ..] This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: A |
last post by:
Hi,
I have always been taught to use an inialization list for initialising data
members of a class. I realize that initialsizing primitives and pointers use
an inialization list is exactly the...
|
by: Mark Neilson |
last post by:
1. What is the best way to make a single instance of my top level class
(DLL) internally available to all other members of the assembly? The top
level object is where all other access is made in...
|
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: philwozza |
last post by:
Hi I have a THREAD class that uses the static variable NextThreadID to
store the id of the next thread to be created and a static Mutex to
protect it.
class THREAD {
public:
int Start(void);...
|
by: ziman137 |
last post by:
Hi all,
The results from following codes got me a bit confused.
#include <stdio.h>
#include <iostream>
using namespace std;
struct A {
|
by: sks |
last post by:
hi ,
i Have a code snippet as follows
class ABC
{
int &r;
ABC(int a=0): r(a) {}
};
int main()
{
|
by: sarathy |
last post by:
Hi,
I read the following points in K&R "Section A8.7 Initialization".
Seems like i have a problem with them.
* All expressions in the initialization of constant object/array must
be constant...
|
by: Jef Driesen |
last post by:
Suppose I have a datastructure (actually it's a graph) with one template
parameter (the property P for each edge and vertex):
struct graph<P>;
struct vertex<P>;
struct edge<P>;
I also have...
|
by: JohnQ |
last post by:
Why would anyone write:
class SomeThing // class littered with non-domain single-instancing code :(
{
private:
SomeThing();
static SomeThing* pInstance_;
public:
static SomeThing*...
|
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: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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: 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...
| |