a little bit offtopic: c++ question in kdevelop-tutorial

Andreas Schlapbach schlpbch at bluewin.ch
Thu Feb 24 23:07:37 GMT 2000


Richard Moore wrote:
> 
> AndiX33 at aol.com wrote:
> >
> > hello,
> >
> > while reading the tutorials shipped with kdevekop, i´ve read an example i
> > don´t understand. and i hope that within this list there are people skilled
> > an experienced in programming.
> >
> > the example:
> >
> >  .....
> > 1       class Math:
> > 2       {
> > 3       public:
> > 4       //Constructor contains definition of PI
> > 5       Math ():  PI (3,142) {}
> > 6       ~Math () {}
> > 7       float  diameter (float radius)
> > 8       {
> > 9       return ( radius * PI )
> > 10      }
> > 11      private:
> > 12      // declare PI. we cann´t assign a value
> > 13      const float PI
> > 14      }
> >
> > what does the ":" in line 4?? is it a kind of operator or what does it
> > it seems strange to me and i didn´t find a solution in books.
> > why you don´t write:
> 
> It calls the constructor for the constant. Obviously this isn't really
> needed for a float, but it is generally good style to do this (it would
> be required if it was a reference to an object with no default constructor).
> IIRC according to the C++ standard this is the recomended way to initialise
> a constant member in any case. Note that it would better code if the constant
> was static anyway and better still if it used the constant defined in math.h.
> 
> Rich.


This is from the C++ FAQ (http://www.cerfnet.com/~mpcline/c++-faq-lite/)
and answer just your questions (I bought the book, and it gave me some
profound insight into C++):

---
[10.6] Should my constructors use "initialization lists" or
"assignment"?

Constructors should initialize all member objects in the initialization
list. 

For example, this constructor initializes member object x_ using an
initialization list: Fred::Fred() : x_(whatever) { }. From a performance
perspective, it is important to note that the whatever expression
doesn't automatically cause a separate object to be created and copied
into x_: if the types are the same the result of ...whatever... will be
constructed directly inside x_. 

In contrast the following constructor uses assignment: Fred::Fred() { x_
= whatever; }. In this case the expression whatever causes a separate,
temporary object to be created, and this temporary object is passed into
the x_ object's assignment operator, then is destructed at the ;. That's
inefficient. 

There's another source of inefficiency as well: in the second
(assignment) case, the object's default constructor (implicitly called
before the constructor body's "{") might, for example, allocate some
default amount of memory or open some default file. All this work could
be for naught if the whatever expression and/or assignment operator
causes the object to close that file and/or release that memory (e.g.,
if the default constructor didn't allocate a large enough pool of memory
or if it opened the wrong file). 

Conclusion: All other things being equal, your code will run faster if
you use initialization lists rather than assignment. 
---

Hope this helps,
Andreas

--
Andreas Schlapbach
mailto:schlpbch at iam.unibe.ch
http://iamexwiwww.unibe.ch/studenten/schlpbch





More information about the KDevelop mailing list