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 5610
tom_usenet wrote: Well, in my poor English vocabulary that is non-determinable. Indeterminate is (for me) something we do not know for sure (like what will rand put there) but we can find out later. Non-determinable is OTOH something which cannot be examined. Indeterminate: a)Not precisely determined, determinable, or established: a person of indeterminate age.
He *has* an age, we just do not know it. It is not like he has no such
thing as an age. While uninitialized PODs may have such bits in their
storage that they have no value whatsoever.
b)Not precisely fixed, as to extent, size, nature, or number: an indeterminate number of plant species in the jungle.
So many, we cannot/will not count it. Again: it is an existing, valid
value.
c)Lacking clarity or precision, as in meaning; vague: an indeterminate turn of phrase.
This - if I understand it properly - is again closer to unknown than to
non-existing (which is the value of an uninitialized POD)
d)Not fixed or known in advance: an indeterminate future.
Again: will be known, only later
e)Not leading up to a definite result or ending: an indeterminate campaign.
This meaning is about an action or series of actions. Cannot really be
applied to a value.
So indeterminate can mean either not precisely determined or not determinable.
Not determinable from the viewpoint of the observer! BIG difference. The
person of indeterminate age *has* an age. A indeterminate campaign *will*
have an end and a result - if not later than on judgment day.
The standard clearly uses it to mean not determinable (since you aren't allowed to examine the value).
I see no such meaning in the listed definitions above. Each and every of
those things above objectively *exist* we just could not, will not, do not
want to determine. OTOH the value of an int, which is built from bits which
makes no sense as an int, is just not there. It does not exist. It is not
an int. There is no sense in talking about it as an int. Since there is no
sense talking about it as an int there is not much sense talking about its
value. There are even more catches to this.
unsigned char c; c = c; //legal!
Should not be. c is used as an rvalue there...
unsigned char can never have indeterminate values (because all bits of the object representation take part in the value representation) .
Where is that required by the standard?
Weird! Why would someone ask for such an arbitrary requirement?
3.9.1/1 Also: http://std.dkuug.dk/jtc1/sc22/wg21/d...ctive.html#129
Hmmmm. This is baaad. I mean this reads: in C++ it is not even not
guaranteed that a variable (POD) has a valid value until it is given one,
but it is not even required to *exist* in its *exclusive* storage... While
I completely agree with what is written in the DR/TR/xR(?) I believe that
this is more than surprising. So not only (real) initialization is optional
but even creation... This is useful elsewhere though: invalid pointers do have indeterminate values, according to the note in 5.3.5/4.
Again a bit of a mystery. delete take a *copy* of the pointer. So it cannot change it at all. So the value of the pointer *is* determined, we exactly know what value it has. Only this value is not to be dereferenced.
It is indeterminate since, although the bit pattern won't have changed, valid bit patterns for pointers might have been changed by the delete.
It is not indeterminate. I know what it is. I can determine it. It is
just not valid to be *dereferenced*. I can even store away together with
sand from the Oregon dunes, my childhood golden hair and photos of my late
girlfriend (she is alive, she just never arrives in time) into an in-memory
application log (possibly as a void*). So the pointer has a *valid* value.
I can copy it around. (at lest I hope so). I just cannot dereference it.
If I cannot (even copy it around - which would make sense) then I dunno
enough C++. :-) Which is true, but this would further prove it. BTW I
never did copy it around, only it could make sense for such logging
purposes.
--
WW aka Attila
"jeffc" <no****@nowhere .com> wrote in message news:<3f******* *@news1.prserv. net>... "foo" <ma*******@axte r.com> wrote in message news:c1******** *************** **@posting.goog le.com... "jeffc" <no****@nowhere .com> wrote in message news:<3f******* *@news1.prserv. net>... "Erik" <no@spam.com> wrote in message news:bj******** **@news.lth.se. .. > > The basic rule of thumb is that anything that can go in the initialize list > > should go there. Obviously, you can't put something like this in there: > > if (a) > > i = 2; > > else > > i = 4; > > Many, if not most, of those cases can be covered by the ?: operator: > MyClass(int a) : i(a ? 2 : 4) {}
Yeah, maybe simple "if" cases like that, but you can't put logic or function calls (other than base class constructors) in the initializer list. You added an expression, not a statement. There are plenty of things you can put in the initializer list. You can put functions, additions, conditon logic, and more.
And there are plenty of things you can't. You can put expressions in there, but you can't put statements in there. You can't call functions (alone), you can't use for or while loops, you can't... well, you already know what you can't do.
Why would you want to put functions alone in an initialize list?
The point of the initialize list is to initialize variables. It's not
for replaceing the constructor's function body.
If you have while-loop logic or any other logic that you need for the
initializaiton of your variable, you can easily put that inside a
function that can be used to initialize the member variable.
Example:
class foo
{
public:
foo(const char* Name, int x)
:m_Msg(SomeFunc 1(Name)), m_Data(SomeFunc 2(Name, x))
{
}
const std::string m_Msg;
const std::string m_Data;
private:
std::string SomeFunc1(const std::string &Name){
return "Hello " + Name;
}
std::string SomeFunc2(const std::string &Name, int x)
{
std::string Data;
for (int i = 0;i < x;++i) Data += Name + " ";
return Data;
}
};
int main(int argc, char* argv[])
{
foo MyFoo("Axter", 3);
cout << MyFoo.m_Msg << endl;
cout << MyFoo.m_Data << endl;
system("pause") ;
return 0;
}
"foo" <ma*******@axte r.com> wrote in message
news:c1******** *************** ***@posting.goo gle.com... "jeffc" <no****@nowhere .com> wrote in message
news:<3f******* *@news1.prserv. net>... "foo" <ma*******@axte r.com> wrote in message There are plenty of things you can put in the initializer list. You can put functions, additions, conditon logic, and more.
And there are plenty of things you can't. You can put expressions in
there, but you can't put statements in there. You can't call functions
(alone), you can't use for or while loops, you can't... well, you already know
what you can't do.
Why would you want to put functions alone in an initialize list?
Check what you wrote 2 posts ago. You're going around in circles.
"Attila Feher" <at**********@l mf.ericsson.se> wrote in message
news:bk******** **@newstree.wis e.edt.ericsson. se... Gary Labowitz wrote: "White Wolf" <wo***@freemail .hu> wrote in message news:bk******** **@phys-news1.kolumbus. fi... Nope. An initialized object of any kind *might* contain an indeterminate value (like initialized by rand()) but it contains a value which is in the value set of the type for that object (object in C sense). Uninitialized (auto) PODs do not fullfill this requirement. If something is not initialized and it might not even be of its own type is uninitialized. Disagree. The type of a variable specifies two things: the size of memory to be accessed and the encoding of the bits in that area.
Yes.
As far as I know, there are not "invalid" combinations of bits for a type.
Wrong. There is nothing in the C++ standard which requires this. A platform is free to represent an integer on 128 bits with 64 bits for
value and 64 bits for checksum.
Whatever is in there is interpreted according to the encoding of the type. It does not necessarily have a meaning. ints aside, this is especially
true with pointers. An implementation is free to encode the pointed type into the pointer's bit value.
The value this results in is usually garbage, but is still a value of the type. Wrong.
Having said that, I wonder if bool types MUST be either all ones or all zeros.
Do not wonder. Read the standard. It does not. And indeterminate value for a bool does not need to represent either true or false. It is in
there. Would a bool containing mixed bits confuse a formatter (like cout) or would it just say "Hmmm, not all zeros --- therefore, true!"
Implementation defined.
Thank you. If this be the case, then all my musings are incorrect. Hmm...
check sum bits in a value. Yes, it could. Then invalid bit combinations
could exist. All resulting in undefined behavior I should think.
Back to the drawing board!!!
[Gee, what did we go back to before they invented drawing boards?]
--
Gary
"Attila Feher" <at**********@l mf.ericsson.se> wrote in message
news:bk******** **@newstree.wis e.edt.ericsson. se... Gary Labowitz wrote: [SNIP] This looks like fun, so I'll take my stab at it. :-) We have way too much time on our hands. ;-)
When a variable is made available to a program, the compiler must have generated code that sets aside some area of memory for use by the program.
OK. Let's change memory to something what can be written in the
standard... let's call it storage.
Now we have to obfuscate your sentence, since if just anybody could understand it (langauge) lawyers (like us ;-) ) would be out of business. Variable is for example a very bad name, because programmers actually understand what it means. Also it is not appropriate here: the process we are going to describe is also applicable to unnamed temporaries, and those are not variables.
In C++ the creation of an object is done in two steps, one of which is optional for objects of POD types. In the first step appropriate (size, alignment) storage is assigned to the object.
If the generated code of the compiler also puts a predetermined value in the area of memory, this is called initialization. The second, optional for POD types, step is initialization.
I.e. the memory allocated to the variable, if examined with (say) a cout << operation, will show a value that the programmer specified in the declaration that requested the allocation. However, if the programmer did not specify an initial value in the declaration, all bets are off. The initial value of the variable could be anything and is usually whatever was in the memory area prior to its being used as the variable being declared.
Without initialization it is not guaranteed that the object represents a valid value for its own type, therefore it is results in undefined
behavior (behaviour for Europeans) if the object is used as an rvalue.
Initialization might happen different ways. Initialization can take the form of explicit initializer or implicit initialization.
For certain structures, the compiler will zero-initialize members implicitly, without the programmer having to specifically declare an initial value. (list cases of implicit initialization)
In the case of arrays, for example, if fewer initial values are provided than the array can store, zero-initialiation will be done for the remaining array elements.
Once the variable is available to the program, any movement of a different value into the variable, such as with an assignment operation, is called assignment.
Storage belonging to objects of POD types might stay in an uninitialized from until an initial rvalue of a correct type has been assigned to it. [ int i; // Storage assigned, uninitialized int j=i; // Undefined behavior, i is not initialized i = 12; // After the ; i is initialized to 12 using assignment ]
The entire issue is: What is in the variable immediately after its "creation?"
An uninitialized object, just like with classes. When the operator new returns it points to a well aligned and big enough storage area, but this storage area does not yet the conform to the requirements of its final
type. If it is a determinate value, specified by the programmer, it has been "initialize d" by the compiler.
And for PODs this initialization is done by the compiler in the form of "first assignment". :-)
[Note: by "memory area" I do not exclude use of registers, data bases, or any other mechanism any given compiler chooses to use for variable storage.]
It is called storage in the standard (for that very reason, that it might not be RAM, which we usually think about when we say memory).
Does this make sense to anyone?
Sort of. :-)
[Last time fully quoted]
Well, I like your obfuscation! I didn't understand anything! It's ready for
publication!
On the serious side, the fact that there are invalid combinations of bits
for types introduces a whole new complexity for me, and makes the standard
almost unbearably complex. Oh well, reality must be faced.
On the whole, I rather prefer Java: nine simple types to learn about, all
well defined with regard to bit usage and size, no surprises.
Thanks for your patience.
--
Gary
"jeffc" <no****@nowhere .com> wrote in message
news:3f******** @news1.prserv.n et... Check what you wrote 2 posts ago. You're going around in circles.
It's called a loop.
--
Gary
White Wolf wrote:
[...] Again a bit of a mystery. delete take a *copy* of the pointer. So it cannot change it at all. So the value of the pointer *is* determined, we exactly know what value it has. Only this value is not to be dereferenced. It is indeterminate since, although the bit pattern won't have changed, valid bit patterns for pointers might have been changed by the delete.
It is not indeterminate. I know what it is. I can determine it. It is just not valid to be *dereferenced*. I can even store away together with sand from the Oregon dunes, my childhood golden hair and photos of my late girlfriend (she is alive, she just never arrives in time) into an in-memory application log (possibly as a void*). So the pointer has a *valid* value.
Nope.
I can copy it around. (at lest I hope so).
Nope. You can't copy it AS POINTER. std::vector<uns igned char> will
work, though. ;-)
regards,
alexander.
Alexander Terekhov wrote: White Wolf wrote: I can copy it around. (at lest I hope so).
Nope. You can't copy it AS POINTER. std::vector<uns igned char> will work, though. ;-)
Not even as void?
--
Attila aka WW
Attila Feher wrote: Alexander Terekhov wrote: White Wolf wrote: I can copy it around. (at lest I hope so).
Nope. You can't copy it AS POINTER. std::vector<uns igned char> will work, though. ;-)
Not even as void?
void pointer? It will also not work. You'll hit undefined
behavior as soon as you try to 'use' deleted/freed pointer
as pointer... assignment of valid pointer value and access
via unsigned char lvalue is totally OK, though. In short,
auto int i; // indeterminate (may be a trap)
auto void * p; // indeterminate (may be a trap)
auto unsigned char c; // indeterminate (unspecified;
// shall NOT be a trap)
IIRC.
regards,
alexander.
On Wed, 17 Sep 2003 15:40:37 +0300, "Attila Feher"
<at**********@l mf.ericsson.se> wrote: Alexander Terekhov wrote: White Wolf wrote: I can copy it around. (at lest I hope so).
Nope. You can't copy it AS POINTER. std::vector<uns igned char> will work, though. ;-)
Not even as void?
This is illegal, even though it isn't dereferenced:
void* p = ::operator new(1);
::operator delete(p);
void* q = p; // boom
You aren't allowed to convert a pointer to an rvalue after deleting
it. You can of course reassign it:
p = 0; //or whatever.
Tom This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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 . . ."
.. . .
|
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
|
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...
|
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 {
| |
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:
|
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:
|
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();
|
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;}
};
|
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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |