using extern to declare an instance of a class.

K

kid_kei

Say there are two classes: System_Controller_Class and UDP_Input_Class.

If I were to declare an instance of class UDP_Input_Class in
System_Controller_Class like so:

extern UDP_Input_Class UDP_Input;

what would be the significance? why not just declare an instance of
the class without the extern? What does adding the extern buy you in
terms of scope?
 
A

Alf P. Steinbach

* kid_kei:
Say there are two classes: System_Controller_Class and UDP_Input_Class.

If I were to declare an instance of class UDP_Input_Class in
System_Controller_Class like so:

extern UDP_Input_Class UDP_Input;

what would be the significance?

Depends on where that declaration occurs.

For a member variable a conforming compiler must reject the code: you
don't use "extern" for member variables.

For a variable declared locally in a member function, it would be a
declaration of an "extern" variable at namespace scope, i.e. a global
variable -- defined somewhere else (most probably in another
translation unit).

why not just declare an instance of the class without the extern?

Only you can answer why you would add "extern".

What does adding the extern buy you in terms of scope?

See above.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top