class object initialisation

Y

Ying Yang

Kevin Goodsell said:
No, it can't. If you believe it can, please give an example.

int i; //declared but not initialised
i =5; // variable i is initialised with value 5.
Well, you can't give something its initial value twice, so I assume you
mean "one of the following ways".

what are you smoking?
...But you're still wrong. Assignment does not give an initial value to
something. Assignment can only happen to an existing object. Since the
object must exist prior to the assignment, it clearly has already
received its initial value.

Assignment can be used for both objects types and primitive types. And i was
referring to initialising data members of a class type. Again, what are you
smoking?
 
Y

Ying Yang

Hi,
You need to have an item in the initialize list if it's a constant or
a reference data member.
You also need to have the base class constructor in the initializer
list if the base class does not have a default contructor.

If my class does not extend any class then there would be no base class your
referring to. Maybe you mean a default constructor of a class must always be
present when you use an initialisation list. Can you be more clear?


PPP
 
K

Karl Heinz Buchegger

Ying said:
No, it's not the only way - as i mentioned you can use an assignment
operator for initialising primitive data members, which is the same as using
an initialization list.

But then it is no longer *initialization*.
It is assignment.

Those are different things.
 
K

Karl Heinz Buchegger

Ying said:
int i; //declared but not initialised

wrong. initialized to an undetermined value. Initialization.
i =5; // variable i is initialised with value 5.

replacing the undetermined value with a determined value. Assignment.

Initialization happens when a variable comes to live. Note: A variable
can also be initialized to an undetermined value! Once a variable
is 'alive', you can't initialize it any more. But of course you
can assign values to it (except in some cases, eg. references).
 
Y

Ying Yang

Ying said:
wrong. initialized to an undetermined value. Initialization.

disagree.

Initialization of an object or primitive means to give a determined value to
them for the first time.

replacing the undetermined value with a determined value. Assignment.

Initialization happens when a variable comes to live. Note: A variable
can also be initialized to an undetermined value! Once a variable
is 'alive', you can't initialize it any more. But of course you
can assign values to it (except in some cases, eg. references).

In light of the above, this is no longer valid.
 
A

Attila Feher

Karl said:
wrong. initialized to an undetermined value. Initialization.


Wrong. This i, if it has an automatic storage, is not initialized.
Accessing it for anything else then writing (assigment or initialization) it
(well, reading it in any way) is undefined behavior.
 
K

Karl Heinz Buchegger

Attila said:

OK.
(it was just a wild guess, apologies for that).

This i, if it has an automatic storage, is not initialized.
Accessing it for anything else then writing (assigment or initialization)

If it has not been initialized, how can it be initialized later?
I thought that initialization can only occour when a variable
is created. Afterwards it is always assignment.
 
T

tom_usenet

disagree.

Initialization of an object or primitive means to give a determined value to
them for the first time.

Your "definition" of initialization is at odds with the C++ standard:

"1 When no initializer is specified for an object of (possibly
cv­qualified) class type (or array thereof), or the
initializer has the form (), the object is initialized as specified in
8.5. [Note: if the class is a non­POD, it is
default­initialized. ]"

Here's the bit in 8.5:
"9 If no initializer is specified for an object, and the object is of
(possibly cv­qualified) non­POD class type (or array thereof), the
object shall be default­initialized; if the object is of
const­qualified type, the underlying class type shall have a
user­declared default constructor. Otherwise, if no initializer is
specified for an object, the object and its subobjects, if any, have
an indeterminate initial value;"

In other words, if you don't explicitly initialize an int (which is a
POD type), it is initialized with an indeterminate initial value.

Tom
 
A

Attila Feher

Karl said:
This i, if it has an automatic storage, is not initialized.

If it has not been initialized, how can it be initialized later?
I thought that initialization can only occour when a variable
is created. Afterwards it is always assignment.

I was not clear. Of course it can only be assigned later on. Except of
course if someone is "clever enough" (remember! we talk about an int, POD)
and call placement new on its address. It would be an initialization,
although a pretty questionable one. :)
 
T

tom_usenet

As previously quoted, 4.1/1 clearly states that an lvalue may not be
used as an rvalue unless it was previously initialized. Apparently,

int n;
n = 0;

qualifies as initialization for these purposes. But it's not done via
direct- or copy-initialization syntax in the declaration of n; rather,
it's done via an assignment expression, so called because it performs
assignment.

That is a defect in the standard. It should say "indeterminate value":

http://std.dkuug.dk/jtc1/sc22/wg21/docs/cwg_active.html#240

The intent of the standard is clear: initialization and assignment are
not ever meant to mean the same thing.

Tom
 
G

Gary Labowitz

Karl Heinz Buchegger said:
OK.
(it was just a wild guess, apologies for that).

This i, if it has an automatic storage, is not initialized. initialization)

If it has not been initialized, how can it be initialized later?
I thought that initialization can only occour when a variable
is created. Afterwards it is always assignment.

Would this allow for assigning a value to a const variable during
construction of the object containing the variable? (Or does it have to be
initialized in the declaration statement?)
Assigning the initial value of a variable (even const, called "final") is
allow in (for example) Java. They appear to keep track of a variable's state
as "uninitialized" and define the assigning of a value during construction
as initialization. Does C++ do this? If it did, then calling a constructor
as part of an initialization list is perfectly valid; and I think it is!
 
T

tom_usenet

Wrong. This i, if it has an automatic storage, is not initialized.
Accessing it for anything else then writing (assigment or initialization) it
(well, reading it in any way) is undefined behavior.

The lvalue to rvalue conversion is undefined for indeterminate values
(use of the term "initialized" has been recognized as a defect in the
standard).

According to other parts of the standard (see my other posts), the
above does count as initialization to an indeterminate value.

Tom
 
T

tom_usenet

And how exactly do you propose calling such a function from the initializer
list?

How do you think?

MyClass(int a): i(someFunction(a))
{
}

someFunction might be a member (static or otherwise), or a namespace
scope function.

Tom
 
A

Attila Feher

tom_usenet said:
specified for an object, the object and its subobjects, if any, have
an indeterminate initial value;"

In other words, if you don't explicitly initialize an int (which is a
POD type), it is initialized with an indeterminate initial value.

No "in other words". They will have an "indeterminate initial value". They
are *not* initialized. Furthermore (let's not force me to look up the
paragraph) accessing such an uninitialized variable is undefined behavior.
(BTW it is in 4.1 paragraph 1)
 
A

Attila Feher

tom_usenet said:
The lvalue to rvalue conversion is undefined for indeterminate values
(use of the term "initialized" has been recognized as a defect in the
standard).

According to other parts of the standard (see my other posts), the
above does count as initialization to an indeterminate value.

If that is what the standard is being changed to, then it is pretty silly.
Initialization is an _action_ being taken. So we say (for automatic/member
PODs): if they are are not initialized then we call this initialization
which never happened as initialization to an indetermined value. Great. It
goes to the deepness of the quantum physics. Furthermore: it has a value,
but that value cannot be read... That suggests me that it is *not*
initialized. It has an indetermined, and non-determinable value. Like the
female-mind vs. male-mind. ;-)
 
A

Agent Mulder

it doesn't matter what your opinion (or mine) is; neither of us will
learn anything about C++ that we didn't already know before this
thread
</>

I do learn from this thread. There are always people lurking. And,
for once, it's on topic!
"Arguing on the Internet is like competing in the Special Olympics...
Even if you win, you're still retarded."

:)

-X
 
T

tom_usenet

No "in other words". They will have an "indeterminate initial value". They
are *not* initialized. Furthermore (let's not force me to look up the
paragraph) accessing such an uninitialized variable is undefined behavior.
(BTW it is in 4.1 paragraph 1)

You snipped these words from my post (reproduced in paraphase):

"When no initializer is specified for an object of class type the
object is initialized as specified in 8.5."

Yes, when you don't give an initializer, it is still initialized.
"Initializer" and "Initialization" are different things. Even if you
don't use an initializer, initialization still takes place.

The lvalue to rvalue thing applies to things that have been
initialized too:

int* p = new int;
delete p; //p now indeterminate (but obviously initialized)
int* q = p; //error, lvalue-to-rvalue conversion

Tom
 
T

tom_usenet

If that is what the standard is being changed to, then it is pretty silly.
Initialization is an _action_ being taken. So we say (for automatic/member
PODs): if they are are not initialized then we call this initialization
which never happened as initialization to an indetermined value. Great. It
goes to the deepness of the quantum physics. Furthermore: it has a value,
but that value cannot be read... That suggests me that it is *not*
initialized. It has an indetermined, and non-determinable value. Like the
female-mind vs. male-mind. ;-)

If you choose your words carefully it makes more sense: "If we don't
provide an initializer for a POD, the object is initialized to an
indeterminate value."

"initialize" and "provide an initializer for" are best thought of as
different things. initialize is something the compiler (or exe) does
to a variable. It does it whether you provide an initializer or not.

Tom
 
A

Agent Mulder

If you choose your words carefully it makes more sense

I am not very into this business of initialization (hard to type)
and assignment, so I try to understand it in a metaphore. The
magician with the White rabbit in the hat. First, there is no hat.
It is flat, doesn't need space. Then, the magician pops up the
hat. It takes up space and provides space. But it is 'empty'.
Is it? Is there a rabbit in the hat?

1. Yes, the rabbit was nicely folded flat in the hat.
2. No, the magician is about to put the rabbit in the hat.

For assignment, putting another rabbit in the hat, we must
visualize a flapdoor at the top of the hat. Is a rabbit POD or
is that not the question?

-X
 
A

Agent Mulder

Excuse me, I must add a third possibility (and a fourth)


I am not very into this business of initialization (hard to type)
and assignment, so I try to understand it in a metaphore. The
magician with the White Rabbit in the hat. First, there is no hat.
It is flat, doesn't need space. Then, the magician pops up the
hat. It takes up space and provides space. But it is 'empty'.
Is it? Is there a rabbit in the hat?

1. Yes, the rabbit was nicely folded flat in the hat.
2. No, the magician is about to put the rabbit in the hat.
3.. No, but the rabbit will jump in by itself
4. The Lovely Lady will put the rabbit in the hat

For assignment, putting another rabbit in the hat, we must
visualize a flapdoor at the top of the hat. Is a rabbit POD or
is that not the question?

-X
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,298
Latest member
ZenLeafCBDSupplement

Latest Threads

Top