struct با static و بدون static - هفت خط کد انجمن پرسش و پاسخ برنامه نویسی

struct با static و بدون static

+3 امتیاز

سلام

آیا این ۲ تا کد فرقی با هم دارن ؟! مگه متغیر وقتی که بصورت global باشه static هم نیست ؟

static struct Range{} range;
struct Range{} range;

 

سوال شده مرداد 19, 1393  بوسیله ی Xavi (امتیاز 627)   24 83 110
دوباره تگ گذاری شد مهر 9, 1393 بوسیله ی BlueBlade

2 پاسخ

+5 امتیاز
 
بهترین پاسخ

کلمه static داخل مثالی که زدین بر می گرده به تعریف متغیر range

متغیری که static  باشه :

1_ فقط داخل Scope ای که تعریف شده قابل استفادست

2_ تا پایان برنامه حافظش پایداره

یعنی اگر این struct با static  گلوبال باشه فقط داخل همین فایل قابل استفاده هست ا

اگر داخل تابع تعریف شده باشه فقط داخل تابع  قابل استفاده هست.

یا اگر این struct رو داخل یک کلاس تعریف می کردین میشد static member و برای استفاده نیاز به ساخت شی از کلاس نداشتین.

استاندارد c++ بخش 3.5 ( N 3690 ) در مورد زمانی که  متغیر  بصورت global تعریف شده باشه

A name is said to have linkage when it might denote the same object, reference, function, type, template,
namespace or value as a name introduced by a declaration in another scope:
— When a name has external linkage , the entity it denotes can be referred to by names from scopes of
other translation units or from other scopes of the same translation unit.
— When a name has internal linkage , the entity it denotes can be referred to by names from other scopes
in the same translation unit.
— When a name has no linkage , the entity it denotes cannot be referred to by names from other scopes.

و 

3 A name having namespace scope (3.3.6) has internal linkage if it is the name of
— a variable, function or function template that is explicitly declared static; or,
— a non-volatile variable that is explicitly declared const or constexpr and neither explicitly declared
extern nor previously declared to have external linkage; or
— a data member of an anonymous union.

و

An unnamed namespace or a namespace declared directly or indirectly within an unnamed namespace has
internal linkage. All other namespaces have external linkage. A name having namespace scope that has not
been given internal linkage above has the same linkage as the enclosing namespace if it is the name of
— a variable; or
— a function; or
— a named class (Clause 9), or an unnamed class defined in a typedef declaration in which the class has
the typedef name for linkage purposes (7.1.3); or
— a named enumeration (7.2), or an unnamed enumeration defined in a typedef declaration in which the
enumeration has the typedef name for linkage purposes (7.1.3); or
— an enumerator belonging to an enumeration with linkage; or
— a template.

 

پاسخ داده شده مرداد 20, 1393 بوسیله ی BlueBlade (امتیاز 15,315)   15 18 89
انتخاب شد مرداد 23, 1393 بوسیله ی مصطفی ساتکی
اگه clause هایی که گذاشتم درست باشه (که برای C++‎ هست) گذاشتن static لازمه. وگرنه اگه در فایل دیگه‌ای متغیری با همین نام تعریف بشه (بسته به کامپایلر) ممکنه lankage error گرفته بشه. اما تا جایی که یادمه آخرین کامپایلر Borland (قبل از انحلال)  در این موارد سخت گیری نمی‌کرد.
آره یکی از دلایلی که بهتره  متغیر global تعریف نشه همین مشکلات مربوط به linker هستش.
+2 امتیاز

فکر می‌کنم بدون  static‏  extern  باشه:

§6.2.2 Linkages of identifiers

* If the declaration of an identifier for an object has file scope and no storage-class specifier,
 its linkage is external.

* If the declaration of an identifier for a function has no storage-class specifier, its linkage is 
determined exactly as if it were declared with the storage-class specifier extern.
 If the declaration of an identifier for an object has file scope and no storage-class specifier,
 its linkage is external.

 

البته C++11 رو نمی‌دونم.

پاسخ داده شده مرداد 19, 1393 بوسیله ی irancplusplus (امتیاز 203)   1 2 14
ویرایش شده مرداد 20, 1393 بوسیله ی irancplusplus
خب این quote ای که از استاندارد گذاشتین که ربطی به این سوال نداره  درباره  تابع هست( identifier for a function)
الان درسته؟
این جمله ها مربوط به چه نسخه ای از استاندارد هستن ؟  آخه من داخل N3690 گشتم این جمله ها رو نتونستم پیدا کنم ؟
مال C++11 نیست (مگه این که تغییری نکرده باشه و بعیده تغییر کرده باشه). در هر حال یه جمله شو سرچ کنید منبع‌های زیادی براش پیدا میشه.
به نظر میرسه جمله های بالا مربوط به استاندارد C هستن نه ++C  
 http://c0x.coding-guidelines.com/6.2.2.html
بعد این که جمله های بالا درباره این هستن که اگر static نباشه چه اتفاقی میفته حرفی درباره static بودن نزدن
خب امتحانش مجانیه یه برنامه با دوتا فایل ‏cpp. درست کنید و در هر دو متغیر a رو بدون static تعریف کنید (همراه با مقدار اولیه). gcc کامپایل نمی‌کنه که یعنی extern حساب می‌شه. اتفاقی که با نذاشتن static  می‌یفته اینه که extern حساب می‌شه.
این که چه اتفاقی میفته رو داخل پست بالاتر توضیح دادم نیاز به آزمایش نیست !
بعد این که داخل سوال ذکر نشده که آیا تعریف این struct آیا داخل .cpp هست یا .h یا اصلا global تعریف شده یا داخل یک تابع یا کلاس دیگه
 پس جواب شما همیشه درست  نیست اگر این struct داخل یک struct یا کلاس  دیگه تعریف شده باشه  extern کاملا بی معنیه .
من این سوال رو جواب دادم: «مگه متغیر وقتی که بصورت global باشه static هم نیست ؟»
آهان هیچی پس من نمیدونم چرا یک مدته حواس پرت شدم :))
...