#### Archived

This topic is now archived and is closed to further replies.

This topic is 5322 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, In Visual Studio 6, C++, the files link in alphabetical order. How do you over ride that? I have 2 global objects A and B. A needs B constructed so it can retrieve information, but since B hasnt been constructed yet, it crashes. Of course I can rename all my classes, but that sounds pretty stupid, and not all the classes are mine to rename anyway thanks Stu PS: anyone know how to define "user defined keywords" in VS6? Cos in options u can change their font colour and that would be ahndy, if i knew how to declare them. [edited by - Yratelev on January 23, 2004 3:16:50 PM]

##### Share on other sites
I''m not sure linking is your problem the way you describe it. If the problem is a crash during the program running because of the order of global variable construction then there''s not much you can do about it. The standard doesn''t specify the order they are constructed in.

what most people end up doing is wrapping them in a class and then contstructing them in the void main(...) method.

Cheers
Chris

##### Share on other sites
You can''t control the construction order of C++ globals (which doesn''t necessarily have anything to do with the order in which they are linked). The general solution is to use a singleton object instead that will create the object on demand.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]

##### Share on other sites
According to the standard, the order in which static objects in different files are linked is unspecified. It is, therefore, bad practice to write C++ programs that depend upon the order in which static objects in different files are linked.

// A.cppextern int B;int A = B;// B.cppint initB (){  return 7;}int B = initB();

In this simplification of your problem, the value of A is undefined.

You have two obvious options. You may put A and B in the same file (via #inclusion, perhaps), or you may check that B is initialised, and initialise it if it is not.

Something along the lines of:

// A.cppextern int getB();int A = getB();// B.cppextern bool initB();bool bIsInitialised = initB();bool initB (){  if (!bIsInitialised)    B = 7;  return bIsInitialised = true;}int B;int getB (){  initB();  return B;}

CoV

##### Share on other sites
Try putting both (all) globals in a class or namespace called "Global". Alternativly, you could use global pointers, which get new-ed at the beginning of the program -- in the correct order. I advocate namespaces though.

-----------
VenDrake

"When you and a friend are being chased by a King Cheata, you have but one chance, trip your friend."

##### Share on other sites
quote:
Original post by VenDrake
Try putting both (all) globals in a class or namespace called "Global".

To put them both in a class would require that they be compiled as the same translation unit. If that were happening, then there wouldn''t be a problem.

Although you can define members of namespaces in seperate files, that doesn''t cause them to be initialised in the correct order.
quote:

Alternativly, you could use global pointers, which get new-ed at the beginning of the program -- in the correct order.

Well, no, they don''t. They get new-ed in an arbitrary order. If you follow this advice, you will crash your program.

A quick tip to posters: don''t post a solution to a problem unless you''ve successfully used that solution yourself.

CoV

##### Share on other sites
quote:
Original post by Mayrel
quote:
Alternativly, you could use global pointers, which get new-ed at the beginning of the program -- in the correct order.

Well, no, they don''t. They get new-ed in an arbitrary order. If you follow this advice, you will crash your program.

i think he meant to use global pointers, and new them at the beginning of main() or WinMain() in the proper order (since global pointers are just that, and don''t create objects unless you tell them to).

anyway, i second the singletons suggestion. that is part of the reason they exist.

##### Share on other sites
Don''t use globals. If you really care about this project than take the effort and pass them as parameters. However, if it''s just a 2 day thing then it doesn''t really matter.

##### Share on other sites
quote:
Original post by Mayrel
quote:
Original post by VenDrake
Try putting both (all) globals in a class or namespace called "Global".

To put them both in a class would require that they be compiled as the same translation unit. If that were happening, then there wouldn''t be a problem.

Although you can define members of namespaces in seperate files, that doesn''t cause them to be initialised in the correct order.

You are correct. By this suggestion, I was infering that the variables need to be declared within the same unit. It''s usually not too hard to make that change. You could simply declare them within the same unit without using a class/namespace, but I find there are hidden advantags to using a container for your globals.

quote:
Original post by Mayrel
quote:
Original post by VenDrake
Alternativly, you could use global pointers, which get new-ed at the beginning of the program -- in the correct order.

Well, no, they don''t. They get new-ed in an arbitrary order. If you follow this advice, you will crash your program.

Maybe I wasn''t clear. "At the beginning of the program" means main() or an initialization function. Basically some place dedicated to non-arbitrary initaliziation.

-----------
VenDrake

"When you and a friend are being chased by a King Cheata, you have but one chance, trip your friend."

##### Share on other sites
if you are using a MS compiler, there are pragmas which you can use to control the order of initialization:
#pragma init_seg (whatever)
GCC has something similar, involving some kind of priority level, but I cant remember it. there have been past posts on this topic - search for them.

1. 1
2. 2
JoeJ
17
3. 3
4. 4
5. 5
frob
11

• 13
• 16
• 13
• 20
• 12
• ### Forum Statistics

• Total Topics
632178
• Total Posts
3004608

×