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,987
  • 10
  • 51
  • 80

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
  • 305,947
  • 44
  • 307
  • 483
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
  • 138,810
  • 21
  • 208
  • 365