开发者

extern pointer problem in c++

I have a header file that has a number of declarations like this:

extern ID3D10Device* device;

I can't make these static because of a problem with multiple translation units, and I can't have them as normal pointers for a similar reason. However when I try to build the program I get unresolved external symbol linker errors. I'm assuming that this is because I'm attempting to use the pointers without defining them first. This is the problem however, as the way you initialise these DirectX objects is by passing the address of the pointers as parameters into specialist methods. - I may be wrong but I am assuming this is the problem as the compiler / linker / whatever can't see the de开发者_运维知识库finitions.

All I'm trying to do is have these pointers (for the graphics device, depth buffer etc) visible to multiple classes. How can this be achieved?


You need the pointers to be defined in some translation unit. The linker is complaining because it seems you haven't done that anywhere. You should declare them at file scope as

ID3D10Device* device = NULL;

in the source file where you call the DirectX function that initializes them. Just make sure the declaration is only made in one source file, then the extern statement should be placed in the associated header file which is included by all translation units that need to use these pointers.


When you externally define a variable like this, the compiler is not reserving any memory for that variable until it sees a definition inside a code module itself. So if you are going to be passing these pointers by-reference to a function for initializing their values, they must be defined in a code module somewhere.

You only need to define them in a single code module ... then place the extern declarations inside a header file you include in the rest of your code modules that require access to the pointer variables. That shouldn't create any linker errors due to duplicate definitions.


I can't make these static because of a problem with multiple translation units

If you need a different variable in different TU (translation units), make it static: this way the variable will be specific to each TU.

A declaration of a variable is also definition unless the extern is used.

You must have one (and only one) definition of a variable in the program.

To have a global variable:

  • Declare it with the extern keyword in some header file.
  • Include this header in every TU that needs to use the variable. Never declare the variable directly, never bypass the header inclusion.
  • Define the variable: a declaration without the extern keyword will define the variable in one TU. You need to include the header file in the same TU to guaranty consistency between the extern declaration and the definition.


the way you initialise these DirectX objects is by passing the address of the pointers as parameters into specialist methods

To solve this part of the problem, you could do something like this (in a .cpp file):

ID3D10Device* device;

struct Foo {
    Foo(ID3D10Device **pdevice) { specialist_method(pdevice); }
};

Foo f(&device);

Beware of the "static initialization order fiasco", though -- it's safe to use device from main, or code called from main, because it will definitely be initialized before that code executes. It's not necessarily safe to use it from other initializers executed before main, because the order of initialization of statics in different translation units is unspecified (within a TU, they're initialized in order of either declaration or definition, I forget which). So device might still be a null pointer in that code. Likewise, specialist_method can't necessarily rely on other statics having been initialized.

There are extra tricks you can use if you need to enforce initialization orders, I'd guess that all the common ones are on SO already in other questions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜