1

I stumbled upon some code like this:

extern Space::MyClass &Global;

I know about extern, but my question is, why would someone put the ampersand there? What's the difference between that and the following?

extern Space::MyClass Global;
Community
  • 1
  • 1
jtpereyda
  • 6,239
  • 8
  • 48
  • 75

2 Answers2

1

The difference is that the one you found has to be declared to refer to something else. Possibly it is part of a conditional-compilation configuration trick.

user207421
  • 298,294
  • 41
  • 291
  • 462
1

The extern must match the actual definition of the variable.

Presumably one of the other units contains:

 Space::MyClass &Global = whatever....;

That means that you have to pick it up with extern Space::MyClass &Global;. Mismatching the types in an extern declaration causes undefined behaviour (no diagnostic required).

M.M
  • 134,614
  • 21
  • 188
  • 335