digitalmars.D - difference between static and nonstatic functions?
- Sean Kelly (3/3) May 17 2004 Besides that function pointers can only point to static (nested)
- Stewart Gordon (30/32) May 19 2004 Welcome to OOP.
- Sean Kelly (10/14) May 20 2004 Ah, that was what I was missing. As C++ doesn't support nested
- Andy Friesen (6/22) May 20 2004 The C concept of static certainly does not apply to D. If you want a
Besides that function pointers can only point to static (nested) functions, is there any difference? Sean
May 17 2004
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
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
Sean Kelly wrote:Stewart Gordon wrote: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) -- andyIf 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?
May 20 2004