473,795 Members | 2,667 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

class object initialisation

A
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 same as an assignment, but for class
types it has a different effect - it calls the copy constructor.

My question is when to not use an initalisation list for initialising data
members of a class?
Regards
Adi

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.516 / Virus Database: 313 - Release Date: 1/09/2003
Jul 19 '05
106 5615
White Wolf wrote:


I do not care if a later "giving of a correct value" is called assignment
and not initialization. Just be brave and admit that it is legal to have
uninitialized PODs around
In the sense that the word is usually used, yes. But it's very clear
that the standard (and the people who wrote it) consider
"initialization " to be something that occurs when the object is created.
I suppose you can debate whether or not it *always* occurs when an
object is created, but that's not really relevant to how the language is
used. The most important point is that initialization cannot happen at
some later time. Assignment can, and then the object is considered, for
all intents and purposes, initialized, and usable as an rvalue.

The only part of this debate that is important at all is the fact that
initialization and assignment are different, and mutually exclusive.
(as long as they are not used as an rvalue) and
that it is legal to assign a value to an uninitialized POD, which makes it
initialized. PODs can be initialized "later" than their
definition/construction.
But it's not initialization in the sense that the standard uses it. It
is (pseudo-)initialization in the sense that the object is not longer
considered "uninitiali zed" (which actually means "indetermin ate" in the
common use).
The initialization (giving a correct initial value
and thereby forming a valid POD type T) can be done using an assigment.


I don't know why we are still debating this when the standard is so
clear on it. Show me anywhere in the standard where it is even suggested
that assignment can constitute initialization.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #61
Alf P. Steinbach wrote:

Appeal to authority is a very very weak argument.
So we can't appeal to the standard and the people who wrote it when
discussing the language they defined?

Especially when that authority himself have used the word in its common
and very much broader meaning, e.g. in the appendix on exception safety
in 3rd ed. of TCPPPL...
So has the standard. It's considered a defect. Stroustrup isn't perfect,
and even if he were his intent in that book is to present the language
in a usable way, not to duplicate the standard. So he uses non-standard
terminology from time to time. But the definition of "initialization "
from his glossary is consistent with the standard.
Also, the standard clearly uses the term "initialization " to describe
something that happens at object creation, not afterward.

The terminology you suggest is very unfortunate, both because it does
not reflect established usage, and because adopting that terminology
would require some new word to refer what's known as initialization.

E.g., in the FAQ the word "initialization " is used to refer to any
initialization whether it occurs during C++ constructor execution or
not, whereas "constructi on" seems to be reserved for C++ constructor
execution.


This is technically an error in the FAQ, but it makes so little
difference that it hardly matters. The only problem with it is that it
leads to discussions like this one. The common usage is technically
incorrect, but rarely harmful.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #62
On Mon, 15 Sep 2003 21:23:16 GMT, Kevin Goodsell <us************ *********@never box.com> wrote:
Alf P. Steinbach wrote:

Appeal to authority is a very very weak argument.
So we can't appeal to the standard and the people who wrote it when
discussing the language they defined?


Those two questions concern a very different matter than redefining
a common term.

The answer to the first is yes, you can.

The answer to the second is yes, you can, but of limited value.

...
The common usage is technically incorrect, but rarely harmful.


Reminds me of Jim Carrey bashing himself in the toilet. ;-)

Jul 19 '05 #63
Alf P. Steinbach wrote:
On Mon, 15 Sep 2003 21:23:16 GMT, Kevin Goodsell <us************ *********@never box.com> wrote:
So we can't appeal to the standard and the people who wrote it when
discussing the language they defined?

Those two questions concern a very different matter than redefining
a common term.


I never suggested that.

The answer to the first is yes, you can.

The answer to the second is yes, you can, but of limited value.


(If I understand you correctly...)

The people that wrote the standard are the only ones who can properly
describe the intent in some cases.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #64
On Mon, 15 Sep 2003 21:55:09 GMT, Kevin Goodsell <us************ *********@never box.com> wrote:
Alf P. Steinbach wrote:
On Mon, 15 Sep 2003 21:23:16 GMT, Kevin Goodsell <us************ *********@never box.com> wrote:
So we can't appeal to the standard and the people who wrote it when
discussing the language they defined?

Those two questions concern a very different matter than redefining
a common term.


I never suggested that.


I thought you did. Hum, well, tracking back up the thread I failed to
see what's really being discussed. Except that calling a non-initialization
initialization is one issue, and there I side with Attila: it's of negative
value to dilute and change a common term by using it for its opposite.

The answer to the first is yes, you can.

The answer to the second is yes, you can, but of limited value.


(If I understand you correctly...)

The people that wrote the standard are the only ones who can properly
describe the intent in some cases.


Is that the case here?

Jul 19 '05 #65
Alf P. Steinbach wrote:

I thought you did. Hum, well, tracking back up the thread I failed to
see what's really being discussed. Except that calling a non-initialization
initialization is one issue, and there I side with Attila: it's of negative
value to dilute and change a common term by using it for its opposite.


I certainly can't deny that, in the past, assigning after the fact to an
object that was declared without an initializer has been considered to
"initialize " it. It does remove the "uninitiali zed" (or "indeterminate" )
status from the object. But in the C++ standard terminology,
"initialization " is considered to be what is done by, e.g., constructors.

The people that wrote the standard are the only ones who can properly
describe the intent in some cases.


Is that the case here?


Could be. If there were an explicit definition for any of the terms
"initialization ", "initialize ", "uninitialized" , etc. then it would be
more clear. I can't find any such definitions though, so I have to
consider the contexts in which the terms are used instead. Based on the
context, the intent would seem to be what I've been claiming (though
maybe not the part about all objects being initialized at declaration,
even without an initializer or appropriate default constructor - I'm not
so sure about that). However, my interpretation of the intent could be
incorrect. So it may be necessary to defer to someone who is in a
position to state what the intent actually was, rather than what is
appears to be.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #66
On Mon, 15 Sep 2003 22:17:00 GMT, Kevin Goodsell <us************ *********@never box.com> wrote:
Alf P. Steinbach wrote:

I thought you did. Hum, well, tracking back up the thread I failed to
see what's really being discussed. Except that calling a non-initialization
initialization is one issue, and there I side with Attila: it's of negative
value to dilute and change a common term by using it for its opposite.
I certainly can't deny that, in the past, assigning after the fact to an
object that was declared without an initializer has been considered to
"initialize " it. It does remove the "uninitiali zed" (or "indeterminate" )
status from the object. But in the C++ standard terminology,
"initializatio n" is considered to be what is done by, e.g., constructors.


Well, it so happens that e.g. §27.4.4.1/2 says, about the std::basic_ios
default constructor:

<quote>
Constructs an object of class basic_ios ... leaving its members

_uninitialized_ .

The object must be

_initialized_

by calling its init member function. If it is destroyed before
it has been

_initialized_

the behavior is undefined.
</quote>

I think it's very simple: the words are used in various contexts, where
the meaning is (generally) clear. The basic meaning is the one in common
use of the term: an act of assigning values after allocation, properly
generalized to include the case of e.g. just assigning a zero value to
the length field of a string implementation, leaving the rest as-is. In
some contexts "initialize " clearly refers to language mechanisms that couple
initialization with allocation, in other cases, as above, initialization
is de-coupled from allocation -- not a technique I recommend, though.

The people that wrote the standard are the only ones who can properly
describe the intent in some cases.


Is that the case here?


Could be. If there were an explicit definition for any of the terms
"initializatio n", "initialize ", "uninitialized" , etc. then it would be
more clear. I can't find any such definitions though, so I have to
consider the contexts in which the terms are used instead. Based on the
context, the intent would seem to be what I've been claiming


See above.

(though
maybe not the part about all objects being initialized at declaration,
even without an initializer or appropriate default constructor - I'm not
so sure about that).


Jul 19 '05 #67
Kevin Goodsell wrote:
Alf P. Steinbach wrote:

Appeal to authority is a very very weak argument.
So we can't appeal to the standard and the people who wrote it when
discussing the language they defined?


You have appealed to Bjarne, not to the standard.
Especially when that authority himself have used the word in its
common and very much broader meaning, e.g. in the appendix on
exception safety in 3rd ed. of TCPPPL...


So has the standard. It's considered a defect. Stroustrup isn't
perfect, and even if he were his intent in that book is to present
the language in a usable way, not to duplicate the standard. So he
uses non-standard terminology from time to time. But the definition
of "initialization " from his glossary is consistent with the standard.


And it is still wrong.
This is technically an error in the FAQ, but it makes so little
difference that it hardly matters. The only problem with it is that it
leads to discussions like this one. The common usage is technically
incorrect, but rarely harmful.


Except that it takes hours, instead of minutes, to make someone understand
that by "initialize d to an undeterminate value" the standard means "not
initialized at all". Especially because the former in itself is a lie. If
the object cannot be read/understood as type T (like int) it does not have a
value. It is not an int yet, so speaking about its value is meaningless.

--
WW aka Attila
Jul 19 '05 #68
Kevin Goodsell wrote:
[SNIP]
The people that wrote the standard are the only ones who can properly
describe the intent in some cases.


Then the wording of the standard (in those cases) is weak. I mean C++ is
complicated enough without making its standard hard to understand.

--
WW aka Attila
Jul 19 '05 #69
Kevin Goodsell wrote:
White Wolf wrote:


I do not care if a later "giving of a correct value" is called
assignment and not initialization. Just be brave and admit that it
is legal to have uninitialized PODs around
In the sense that the word is usually used, yes. But it's very clear
that the standard (and the people who wrote it) consider
"initialization " to be something that occurs when the object is
created.


Except when not. Read Alf's post with his quote from the standard library
docs, where the standard definitely says that a constructor "leaves members
uninitialized". And the "initializa tion is done later".
I suppose you can debate whether or not it *always* occurs when an
object is created, but that's not really relevant to how the language
is used. The most important point is that initialization cannot
happen at some later time.
You can write it down to 101 standards, it still *does* happen later. If
you get an auto bool it is *not* a bool if you do not give it an initializer
until you assing a bool value to it later. If it is not a bool, then it is
definitely not initialized. PODs can be initialized later. Otherwise CPUs
would need to support constructors.
Assignment can, and then the object is considered,
for all intents and purposes, initialized, and usable as an rvalue.
In other words: initialized.
The only part of this debate that is important at all is the fact that
initialization and assignment are different, and mutually exclusive.
No, in case of PODs they are not very different and mutually exclusive:

int i =1; does exaclty the same as
int i;
i=1;
But it's not initialization in the sense that the standard uses it.
My point is that the standard uses the word incorrectly. So it would help
not repeating it forever that the standard uses it. So what? Even I made
mistakes. It was long time ago, but I did. ;-))
It
is (pseudo-)initialization in the sense that the object is not longer
considered "uninitiali zed" (which actually means "indetermin ate" in
the common use).
Nope. It is initialization. Not syntax-wise maybe not C++ semantics-wise
as the standard defined it, but it is initialization. It gives the POD
object its first value. That is called initialization.
The initialization (giving a correct initial value
and thereby forming a valid POD type T) can be done using an
assigment.


I don't know why we are still debating this when the standard is so
clear on it.


Because something wrong clearly said is still wrong. And I care about logic
and not about authority.
Show me anywhere in the standard where it is even
suggested that assignment can constitute initialization.


I do not give a damn if it is done or not. Just because it has ISO or ANSI
on it it is not a Holy Book from God. This is an error. Those - who wrote
that part - wanted to make a very very clear difference between
initialization and assignment. In case of non-const POD types there is no
such difference. So they came up with a hard-to-get clouding statement
which finally says: if you did not initialize a POD it is not yet
initialized. You can initialize it using assignment later.

--
WW aka Attila
Jul 19 '05 #70

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

Similar topics

1
3940
by: matro | last post by:
hi there, I have the following struct in my class: class CTbStDialog : public CDialog { public: int fooVar, fooVar2; ... //other foo variables...
8
2536
by: jose luis fernandez diaz | last post by:
Hi, I am reading Stroustrup's book 'C++ Programming Language'. In the 10.4.9 section (Nonlocal Store) he says: "A variable defined outside any function (that is global, namespace, and class static variables) is initializated (constructed) before main is invoked . . ." .. . .
11
2559
by: Neil Zanella | last post by:
Hello, When an "std::map<int, int> foobar;" statement appears in a C++ program followed by a statement of the form "foobar;" where nothing has ever been inserted at position 123 into map foobar, the C++ standard states that a new element is created at position 123 and is initialized to zero. That is, in this case, the pair (123, 0) is inserted into the map. I am interested in knowing what the standard says should happen in the
3
1282
by: deancoo | last post by:
I'm hoping this isn't too off topic, but I'm not sure if I've included some of my member functions in the right class. I needed to develop numerous functions to help define object A. These functions currently exist in the class definition for object A. The thing is, there is another object (B) who's job it is to call these functions when the app starts up, defining all possibilities for object A's, then allow future object A's to look up...
13
2075
by: Frederick Gotham | last post by:
I have just been reading 8.5 in the Standard, and am trying to make sense of the different kinds of initialisations. Up until now, I thought of an object as either NOT being initialised (i.e. containing garbage), or being default initialised (i.e. containing the default value for that type). Here are some examples of the former: struct MyStruct {
16
1477
by: silversurfer2025 | last post by:
Hello everyone, once again, I have a very basic problem in C++, which I was not able to solve (maybe because of my Java-Experience or just because it is always the small syntax-things which break my neck in C++)... I would like to have a static variable in class A and change its value from class B (they are not related to each other) For example:
14
2584
by: Jeroen | last post by:
Hi all, I've got a question about writing a library. Let me characterize that library by the following: * there is a class A which is available to the user * there is a class B that is used in severel 'underwater operations' * there is a list which stores objects of class B There are several issues I'm not sure about:
2
1904
by: .rhavin grobert | last post by:
i have (do try to have?) the following... & = breakpoints in debugger // ---------------------------------------------------------------- // cx.h class CX { public: CX(CX* pcx = NULL); virtual ~CX();
13
1658
by: Anarki | last post by:
#include <iostream> using namespace std; class ConstantMember { const int m_const; public: ConstantMember(int cons = 10):m_const(cons){} int getConst() const{ return m_const;} };
0
9672
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10436
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10163
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3722
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.