PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > initializer Vs assignment list in constructor
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
initializer Vs assignment list in constructor

Réponse
 
LinkBack Outils de la discussion
Vieux 16/01/2008, 06h51   #1
Pallav singh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut initializer Vs assignment list in constructor

How can we justify that initializer list is better in performance
than assignment list in constructor of C++ ??
  Réponse avec citation
Vieux 16/01/2008, 07h09   #2
Jack Klein
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: initializer Vs assignment list in constructor

On Tue, 15 Jan 2008 22:51:15 -0800 (PST), Pallav singh
<singh.pallav@gmail.com> wrote in comp.lang.c++:

> How can we justify that initializer list is better in performance
> than assignment list in constructor of C++ ??


Why would we want to justify it? The C++ standard does not specify or
require such a thing.

An initializer list is the preferred way to initialize the members in
a constructor. It is the only possible way to initialize reference
members and constant members. So just use initializer lists.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
  Réponse avec citation
Vieux 16/01/2008, 10h54   #3
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: initializer Vs assignment list in constructor

On Jan 16, 8:09 am, Jack Klein <jackkl...@spamcop.net> wrote:
> On Tue, 15 Jan 2008 22:51:15 -0800 (PST), Pallav singh
> <singh.pal...@gmail.com> wrote in comp.lang.c++:


> > How can we justify that initializer list is better in
> > performance than assignment list in constructor of C++ ??


> Why would we want to justify it? The C++ standard does not
> specify or require such a thing.


> An initializer list is the preferred way to initialize the
> members in a constructor. It is the only possible way to
> initialize reference members and constant members. So just
> use initializer lists.


You might add that by using an initializer list, you reduce the
chances of accidentally using the variable before it has been
initialized. It's part of the larger philosophy of never
defining a variable without initializing it.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
  Réponse avec citation
Vieux 16/01/2008, 17h58   #4
Erik Wikström
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: initializer Vs assignment list in constructor

On 2008-01-16 08:09, Jack Klein wrote:
> On Tue, 15 Jan 2008 22:51:15 -0800 (PST), Pallav singh
> <singh.pallav@gmail.com> wrote in comp.lang.c++:
>
>> How can we justify that initializer list is better in performance
>> than assignment list in constructor of C++ ??

>
> Why would we want to justify it? The C++ standard does not specify or
> require such a thing.


No it does not, but it does guarantee some other things that makes it
very probably that using the initialiser-list if slightly more
efficient. Consider a class like this:

class Foo
{
Bar bar;
Baz baz;

public:
Foo(int a,int b);
}

And assume that both Bar and Baz each have constructors taking an
integer, a default constructor, and a assignment operator.

Before the body of the constructor starts to execute all members of the
class have to be initialised (so that they are complete objects when the
body of the constructor executes). If you want bar and baz to be objects
created with a and b passed to their constructors and do not use an
initialisation-list you would have to do something like this:

Foo::Foo(int a, int b)
{
bar = Bar(a);
baz = Baz(b);
}

Or alternatively use bar.set(a) or something like that. This means that
in a worst case scenario you have to run the constructors of Bar and Baz
twice just to create one Foo object. If you use an initialisation-list
instead bar and baz will be constructed with the correct parameters
before the body of Foo's constructor is executed.

You can never get better performance by not using initialisation-lists,
but you can sometimes get the same performance.

--
Erik Wikström
  Réponse avec citation
Vieux 17/01/2008, 09h19   #5
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: initializer Vs assignment list in constructor

On Jan 16, 6:58 pm, Erik Wikström <Erik-wikst...@telia.com> wrote:
> On 2008-01-16 08:09, Jack Klein wrote:


> > On Tue, 15 Jan 2008 22:51:15 -0800 (PST), Pallav singh
> > <singh.pal...@gmail.com> wrote in comp.lang.c++:


> >> How can we justify that initializer list is better in
> >> performance than assignment list in constructor of C++ ??


> > Why would we want to justify it? The C++ standard does not
> > specify or require such a thing.


> No it does not, but it does guarantee some other things that
> makes it very probably that using the initialiser-list if
> slightly more efficient.


Or not. The cases where there is a measurable difference are
probably fairly rare.

[...]
> You can never get better performance by not using
> initialisation-lists, but you can sometimes get the same
> performance.


I wouldn't say never---if I tried, I'm sure I could write some
perverse code where default construction followed by assignment
was faster than direct construction. Most of the time, however,
there's just no difference.

Performance isn't the motivation for initializer lists, however.
The motivation is security---not having uninitialized variables
floating around. (This isn't quite true, as the initialization
expressions can reference other member variables---including
those not yet initialized. But using initialization lists does
reduce the risk enormously.)

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
  Réponse avec citation
Vieux 18/01/2008, 18h13   #6
Marc
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: initializer Vs assignment list in constructor

> How can we justify that initializer list is better in performance
> than assignment list in constructor of C++ ??


http://www.parashift.com/c++-faq-lit....html#faq-10.6

--
Marc
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h16.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,16752 seconds with 14 queries