New topic: static variable within a class method?
<http://forums.realsoftware.com/viewtopic.php?t=30685> Page 1 of 1 [ 4 posts ] Previous topic | Next topic Author Message JustSomeGuy Post subject: static variable within a class method?Posted: Mon Oct 26, 2009 9:07 pm Joined: Fri May 11, 2007 11:35 am Posts: 547 I know I can have a member property that a method in a class can use, but can I have a static variable IN a method that is only visible withing the methods of the instance and retains its value between invocations? In c: Code:void myClass::method(void) { static int counter = 0; cntr++; } _________________ A picture paints a thousand words. A thought paints a thousand pictures. Top piMaster Post subject: Re: static variable within a class method?Posted: Mon Oct 26, 2009 9:32 pm Joined: Fri Sep 30, 2005 9:12 am Posts: 672 Location: Rockford, IL Code:Static Counter as Integer _________________ RB2009r4 Pro on Win XP Top timhare Post subject: Re: static variable within a class method?Posted: Mon Oct 26, 2009 10:25 pm Joined: Fri Jan 06, 2006 3:21 pm Posts: 6622 Location: Portland, OR USA Be aware that the static variable is shared between all instances of the class. If you want a static variable unique to each instance, use a normal property and mark it private. Top Phil M Post subject: Re: static variable within a class method?Posted: Tue Oct 27, 2009 12:06 am Joined: Fri Sep 30, 2005 12:18 pm Posts: 220 My favorite use of static is in custom controls where I "render" the display of the control (or part of it) the first time, and then its always available from then on. =) Yeah pictures take a lot of memory, but its usually little tiny elements. =) Top Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending Page 1 of 1 [ 4 posts ] -- Over 1500 classes with 29000 functions in one REALbasic plug-in collection. The Monkeybread Software Realbasic Plugin v9.3. http://www.monkeybreadsoftware.de/realbasic/plugins.shtml [email protected]
