Mail Archives: djgpp/1995/01/15/01:21:21
Sorry if this message has appeared before on this list. I posted the question
a few days ago, but for a variety of reasons I suspect it didn't get
through.
Anyway, I'm having a problem with the order in which constructors are
invoked for global C++ objects. I have two classes, say A and B, and one
global object for each class:
class A {
public:
A() {constructor code;}
};
A a;
class B {
public:
B() {do something with object a;}
};
B b;
Now, the problem is, I need the constructor for a to be invoked before the
constructor for b (since this constructor uses object a). This is complicated
by the fact that the objects are in different source files in an archive.
Is there any way that I can force the above ordering? I've tried changing the
order of the files in the archive, which I though might change the order in
which they were linked, but this didn't help.
I wrote a little test program to try and solve this problem, and found that
I had to link the object files in the _reverse_ order that I wanted the
constructor invoked. Unfortunately, ordering the files in the archive in
the reverse order didn't work :(
Just another comment about this. Do people think that the above kind of
code is bad programming style? (ie. having global objects, with a requirement
to have a certain ordering for the invokation of their constructors.) I do
have good reasons for doing it, but I just wondered what people thought.
Thanks for any ideas.
Chris cgg AT mundil DOT cs DOT mu DOT oz DOT au
- Raw text -