www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - difference between static and nonstatic functions?

reply Sean Kelly <sean ffwd.cx> writes:
Besides that function pointers can only point to static (nested) 
functions, is there any difference?

Sean
May 17 2004
parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Sean Kelly wrote:

 Besides that function pointers can only point to static (nested) 
 functions, is there any difference?
Welcome to OOP. A static function is one that is a member of a class, but operates on the class itself, not on an object of the class. Just as a static variable belongs to the class, rather than to each object of the class. Hence there is no concept of 'this' in a static function. class Qwert { static int yuiop; int asdfg; static void hjkl() { printf("%d\n", yuiop); // valid printf("%d\n", asdfg); // invalid } void zxcvb() { printf("%d\n", yuiop); // valid printf("%d\n", asdfg); // also valid } static void nm() { hjkl(); // valid zxcvb(); // invalid } } If you're nesting functions within functions, then the same applies. Here, the concept of 'object' is more or less replaced by 'stack frame', corresponding to a call to the enclosing function. Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit.
May 19 2004
parent reply Sean Kelly <sean ffwd.cx> writes:
Stewart Gordon wrote:
 
 If you're nesting functions within functions, then the same applies. 
 Here, the concept of 'object' is more or less replaced by 'stack frame', 
 corresponding to a call to the enclosing function.
Ah, that was what I was missing. As C++ doesn't support nested functions I wasn't sure how the keyword applied outside of the class context. Just to clarify, how does the classic definition of "static" as it applies to global symbols in C operate in D? If I declare a global variable or free function static, does it have any effect at all? Can I still assume that a D module and its imports rougly corresponds to a translation unit and that global statics will follow the classic visibility rules? Sean
May 20 2004
parent Andy Friesen <andy ikagames.com> writes:
Sean Kelly wrote:
 Stewart Gordon wrote:
 
 If you're nesting functions within functions, then the same applies. 
 Here, the concept of 'object' is more or less replaced by 'stack 
 frame', corresponding to a call to the enclosing function.
Ah, that was what I was missing. As C++ doesn't support nested functions I wasn't sure how the keyword applied outside of the class context. Just to clarify, how does the classic definition of "static" as it applies to global symbols in C operate in D? If I declare a global variable or free function static, does it have any effect at all? Can I still assume that a D module and its imports rougly corresponds to a translation unit and that global statics will follow the classic visibility rules?
The C concept of static certainly does not apply to D. If you want a symbol to be local to the module, use the 'private' access modifier instead. I think static is ignored in the global scope. (I'm not sure if it ought to be a syntax error, as it might be handy in the case of mixins) -- andy
May 20 2004