21-11-2004, 22:16
|
|
|
חבר מתאריך: 20.12.01
הודעות: 20,962
|
|
תגיד לי, מה הוכחת בזה?
Vandelay שאל:
ציטוט:
במקור נכתב על ידי Vandelay
2. מה המשמעות של פונקציות סטטית ב ++C? ומה ההבדל בין משתנים סטטים ב C ל ++C?
|
Gh0sT_d0g תמה על השאלה, כי הוא לא שמע על הבדל בין משתנים סטטים ב-C למשתנים סטטיים ב-CPP(משתנים סטטיים כלומר משתנים סטטים של פונקציה, ולא משתנים חברים סטטיים של מחלקה כמובן, כי ב-C אין מחלקות)
ציטוט:
במקור נכתב על ידי Gh0sT_d0g
לא ידעתי שיש הבדל..
|
scripter חיזק את דבריו של Gh0sT_d0g
ציטוט:
במקור נכתב על ידי scripter
מלבד ההבדל של internal linkage, לא עולה לי לראש משהו אחר
אשמח אם Vandelay, יוכל להביא מקור רשמי (תקנים לפחות) שמדבר על הנושא.
|
Vandelay ניסה לתקן באמרו:
ציטוט:
במקור נכתב על ידי Vandelay
הכוונה היתה להבדלים בין פונקציות סטטיות.
|
רק שהתיקון לא ממש הגיוני:
ב-C אין פונקציות סטטיות(פונקציה סטטית - הכוונה פונקציה חברה סטטית של מחלקה, וב-C הרי אין מחלקות).
כיצד אפשר להשוות בין פונקציות סטטיות ב-C לבין פונקציות סטטיות ב-CPP אם הן כלל לא קיימות ב-C?...
וכאן אתה בא ואומר משהו שלא ממש ברור לי מה המטרה שלו...
אתה מנסה לחזק את דברי ולהגיד "כן, באמת יש רק ב-CPP ולא ב-C", או מה?
אני באמת ובתמים לא מבין מה אתה בא להגיד לי פה...
ואם כבר הזכרת את הנושא, פונקציה סטטית, משמעו:
Static Member Functions
Static member functions are considered to have class scope. In contrast to nonstatic member functions, these functions have no implicit this argument; therefore, they can use only static data members, enumerators, or nested types directly. Static member functions can be accessed without using an object of the corresponding class type.
...
Static member functions have external linkage. These functions do not have this pointers. As a result, the following restrictions apply to such functions: - They cannot access nonstatic class member data using the member-selection operators (. or >).
- They cannot be declared as virtual.
- They cannot have the same name as a nonstatic function that has the same argument types.
C++ Tutorial - Static Functions:
Static data types can be accessed without instantiation of the class in C++. This is applicable for static functions also.
The differences between a static member function and non-static member functions are as follows.
- A static member function can access only static member data, static member functions and data and functions outside the class. A non-static member function can access all of the above including the static data member.
- A static member function can be called, even when a class is not instantiated, a non-static member function can be called only after instantiating the class as an object.
- A static member function cannot be declared virtual, whereas a non-static member functions can be declared as virtual
- A static member function cannot have access to the 'this' pointer of the class.
The static member functions are not used very frequently in programs. But nevertheless, they become useful whenever we need to have functions which are accessible even when the class is not instantiated.
7.2: Static member functions
Besides static data, C++ allows the definition of static functions. Similar to the concept of static data, in which these variables are shared by all objects of the class, static functions apply to all objects of the class.
The static functions can therefore address only the static data of a class; non-static data are unavailable to these functions. If non-static data could be addressed, to which object would they belong? Similarly, static functions cannot call non-static functions of the class. All this is caused by the fact that static functions have no this pointer.
Functions which are static and which are declared in the public section of a class interface can be called without specifying an object of the class.
תהנה
|