Path: utzoo!attcan!uunet!ogicse!uwm.edu!cs.utexas.edu!samsung!munnari.oz.au!cluster!metro!otc!ruben From: ruben@otc.otca.oz (Ruben Gonzalez) Newsgroups: comp.lang.c++ Subject: implementation of static local variables Keywords: static local variable Message-ID: <1350@otc.otca.oz> Date: 14 Mar 90 06:11:01 GMT Lines: 46 The general argument for the existence of local static variables primarily involves that of modular cohesiveness. In languages which do not support local static variables, any variable whose value must persist across function invocations must be globally defined, this destroys modular cohesivness. The problem arises in C++ with the way in which static local variables are implemented for non static member functions. At the moment any static local variable (SLV) belonging to a non static member function is treated as if it were a normal static member variable of the object of which the function is a member. Therefore it will be accessed and modified by all instances of that object. SLVs within member functions are therefore made redundant by normal static member variables and should not exist. There is apparently no way to implement a local variable within a member function of an object which persists across invocations whose scope is limited to that particular instance of the object. As far as I have been able to asertain the only way round this is to create a non static member variable, and to use that. The problem is that modular cohesion is destroyed. Why should I have to make a variable visible to an entire object when it is only used by one member function? This is like having to make a variable in PASCAL global because SLVs are not implemented. The implementation at the moment for SLVs as mentioned is redundant and at any rate it transgresses the concept of having independent instances of an object. Why can't the implementation of SLVs not be such that their scope is restricted to only the instance of the object in which the owner function occurs. Thus maintaining the validity of the main argument in favour of the existence of SLVs. As it stands, it appears to be a blotch on C++s otherwise impressive implementation. If for some reason there is a better way so that I don't have to violate the most basic rule in the book about modular programming, or at least a half decent reason for why it must be so, please let me know. In the Interests of Better Programming. Ruben Gonzalez.