Topic: Initailization order of static members of base and derived classes


Author: wmm@fastdial.net
Date: 2000/11/13
Raw View
In article <z4JO5.5814$mq1.401480@bgtnsc04-news.ops.worldnet.att.net>,
  "Joseph Gottman" <joegottman@worldnet.att.net> wrote:
> Suppose I have two classes, defined in different translation units:
> class Base {
>  static int  BaseValue;
> };
>
> class Derived : public Base {
>  static int DerivedValue;
> };
>
> Can I assume that Base::BaseValue will be created before
> Derived::DerivedValue?  I know that in general the order of creation
of
> static objects in different translation units is undefined.  Is still
true
> when one class is derived from another?

The order of initialization depends on where the static members
are defined, not on the relationship of their classes.  If both
are defined in the same translation unit, the order of
initialization will be the same as the order in which their
definitions appear.  If they appear in different translation
units, there's no guaranteed order.

--
William M. Miller, wmm@fastdial.net
Vignette Corporation (www.vignette.com)


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: "Joseph Gottman" <joegottman@worldnet.att.net>
Date: 2000/11/10
Raw View
Suppose I have two classes, defined in different translation units:
class Base {
 static int  BaseValue;
};

class Derived : public Base {
 static int DerivedValue;
};

Can I assume that Base::BaseValue will be created before
Derived::DerivedValue?  I know that in general the order of creation of
static objects in different translation units is undefined.  Is still true
when one class is derived from another?


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: Ron Natalie <ron@sensor.com>
Date: 2000/11/10
Raw View

Joseph Gottman wrote:
>
> Suppose I have two classes, defined in different translation units:
> class Base {
>  static int  BaseValue;
> };
>
> class Derived : public Base {
>  static int DerivedValue;
> };
>

These can't be in different translation units.  You can't derive from
an incomplete class definition.  Perhaps what you mean is that the
static variables is defined in two separate translation units.

> Can I assume that Base::BaseValue will be created before
> Derived::DerivedValue?

No.  As far as the static concerned, it's just another non-local
object in whatever TU.  It's initialization is determined only
within the TU by the location of the definition.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]