473,320 Members | 1,910 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

When is it nesessary with initialize list

Hello!!

Hello Experts!

I'm right if I say that if I have attribute that is constant or attribute
that is of reference type or attribute that is class type then I must use
initialize list.

//Tony
Sep 9 '05 #1
3 2064

"Tony Johansson" <jo*****************@telia.com> wrote in message
news:rk*********************@newsc.telia.net...
Hello!!

Hello Experts!

I'm right if I say that if I have attribute that is constant or attribute
that is of reference type or attribute that is class type then I must use
initialize list.

//Tony


If the member variable (what you've called "attribute") is a class object,
then you _can_ initialze it via the member initializer list, but you don't
_have_ to. If it exists, or can be generated by the compiler then, the
default constructor for the contained class object will be called during
construction of the containing class. But if there is no default
constructor, or if the compiler is unable to generate one (because you've
declared one or more constructors yourself), then yes, you will need to
construct it via the member initializer list. (And you may want to, anyway,
since it's the only way to pass parameters to that object's constructor.)

-Howard
Sep 9 '05 #2
Tony Johansson wrote:

Hello!!

Hello Experts!

I'm right if I say that if I have attribute that is constant or attribute
that is of reference type or attribute that is class type then I must use
initialize list.


You are right with the first 2 but the third case is different. Depending
on the exact configuration of constructors it is possible to let the
compiler use the default constructor for the initialization and later
on assign in the constructor.

But you get it backwards.

You *should* use initializer lists until there is no way to do
what you want with an initializer list (eg. initializing
an array)

--
Karl Heinz Buchegger
kb******@gascad.at
Sep 9 '05 #3
Tony Johansson wrote:
I'm right if I say that if I have attribute that is constant or attribute
that is of reference type or attribute that is class type then I must use
initialize list.


If your const "attribute" is of class type that has a default c-tor, then
you don't have to, if default-initialisation is OK for it. Of course, in
many cases default initialisation is not OK, then you need to initialise
it in the constructor initialiser list. References need to always be
explicitly initialised.

There is a pitfall in all that. If you have a member _array_ of some type
that doesn't have a default c-tor, there is no way to initialise it in the
constructor initialiser list, const or non-const.

V
Sep 9 '05 #4

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

Similar topics

1
by: Gerry Sutton | last post by:
Hi All! I have noticed a strange behavior when using a constant identifier to initialize an instance list variable in a base class and then trying to modifying the list in subclasses by using...
10
by: Fred Ma | last post by:
Are there any reasons that would make it bad for C++ to allow simultaneous declaration and initilization of member data? Current way: ------------ class DerivedClass : BaseClass { { enum {...
5
by: Jim Langston | last post by:
What I want to do: have a vector of ints in my class initialized with 0 to 499 which will later be pushed/popped out of the vector by instances. What I have: class CParticleStream // Yes, I...
6
by: Ramprasad A Padmanabhan | last post by:
I have a simple structure defined like this struct userid { char uid; int insize; int outsize; }; typedef struct userid user;
6
by: Ron Vecchi | last post by:
I need to allow my control when it is visible to capture the mouse and not let any other control react to mouse events until control is hidden. Similar to ContextMenu Any Suggestions --...
4
by: A Traveler | last post by:
Hello all, I have a control i wrote which inherits from asp:label. It worked fine. It extends asp:label with a "For" property which implements the "for" attribute of an html label tag (yes i...
2
by: tony | last post by:
Hello! I'm trying to build a class library which has a class called AvestaPlantFunc. In this project building a class libray exist a class called AvestaPlantFunc. In this class is there a...
13
by: Kantha | last post by:
Hi all, I have declared an Union as follows typedef union { struct interrupt_bits { unsigned char c_int_hs_fs_status : 1, c_setup_intflag : 1,
8
by: defn noob | last post by:
isPrime works when just calling a nbr but not when iterating on a list, why? adding x=1 makes it work though but why do I have to add it? Is there a cleaner way to do it? def isPrime(nbr):...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
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 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.