Author: Steffen Jakob
Date: 12:48:42 06/12/02
Go up one level in this thread
On June 12, 2002 at 15:35:23, Dann Corbit wrote:
>On June 12, 2002 at 15:33:23, Steffen Jakob wrote:
>[snip]
>>I think I dont understand. Do you think in the following code the default ctor
>>of class C would cost time? You c++ compiler should optimize this call away (at
>>leas g++ 3.0.2 which i have installed here does so).
>>
>>class C {
>>public:
>> C() { /* do nothing and leave n uninitialized. */ }
>> C(int _n) : n(_n) { }
>>
>> void set(int _n) { n = _n; }
>> const int &get() const { return n; }
>>
>>private:
>> int n;
>>};
>>
>>int main(int argc, char **argv) {
>> C c; // triggers the default ctor
>> return c.get(); // do something with c
>>}
>
>A nice illustration of why constructors should always initialize variables.
>This program (as written) exhibits undefined behavior.
Yes, that was intentionally :) BTW, do you know g++'s option -Weffc++? Here it
spits out:
t.cpp:3: warning: `C::n' should be initialized in the member initialization
list
This option checks some advices from Scott Meyers great "Effective C++" books.
Unfortunately the standard c++ library produces a huge amount of warnings if you
use this in a more complex program (I have a tricky grep statement to get some
reasonable output anyway).
Greetings,
Steffen.
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.