What does the extern keyword mean?

后端 未结 3 1781
梦谈多话
梦谈多话 2020-12-07 21:56

What does the extern keyword mean? I\'ve seen that in front of an function declaration like

extern void DoFoo ...
3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-07 22:54

    For Beginners,

    Initially I was confused to learn that, "the extern keyword declares a variable or function and specifies that it has external linkage " by @Romain Hippeau.

    Now I understood that, we will be able to share our variables with other classes through extern keyword.

    For Example: Notification.h

     #import 
     extern const NSString* notificationConstant;
    

    Notification.m

     #import "Notification.h"
     const NSString* notificationConstant = @"NotificationConstant";
    

    By importing notification.h in any of my other classes, I can read the value of string NotificationConstant.

    Without extern keyword For Notification constant will create following error.

提交回复
热议问题