Author: Dann Corbit
Date: 12:35:23 06/12/02
Go up one level in this thread
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.
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.