jtpereyda
jtpereyda

Reputation: 7395

Why make a extern variable a reference?

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;

Upvotes: 1

Views: 83

Answers (2)

M.M
M.M

Reputation: 141633

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).

Upvotes: 1

user207421
user207421

Reputation: 311052

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.

Upvotes: 1

Related Questions