TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: NEIL HELLER
from: DARIN MCBRIDE
date: 1997-03-19 21:33:00
subject: STATIC? SAY WHAT?

DM> class c
DM> {
DM> public: // for simplicity
 NH>  
DM>   static void foo();
DM>   void bar();
DM> private:
DM>   static int foo_val;
DM>   int bar_val;
DM> };
DM> c::bar_val : from anywhere in namespace c with a valid object of
DM> type c.  This includes foo() _IFF_ foo() has a valid c object.  It
DM> can then have c_obj.bar_val directly, it has "access" to the
DM> private members since it is itself a member of the class.
 NH> Can you think of any situations in which this method of access to the
 NH> value of bar_val is the most preferred or the only way?  Do you think
From a static member?  I can't think of one.
 NH> this method of access of the value of bar_val is in keeping with the
 NH> spirit of C++?  I'm just curious?
Yes, I do.  It keeps access to these variables in the place where access is 
required - its members.
 NH> BTW, when is it ever really necessary (not just possible) to use
 NH> double-colons outside of method headers?
It is ALWAYS necessary outside of method headers.  In order to define 
c::bar(), you HAVE to use:
void c::bar()
{
// code
}
if you are to do it outside of the class headers.  Another place would be if 
the class had a typedef or another class in it:
class linklist
{
public:
  class iterator
  {
   // iterator stuff
  }
  // other stuff (private, too)
};
In order to use this iterator type you MUST use:
linklist::iterator it_obj;
The benefits of this are tremendous!  Think about this for a second:
typedef linklist container;
container::iterator i;
Later, you decide that a linklist is overkill, so you switch your 
implementation to, say, a double-ended queue which has an iterator that acts 
the same as linklist::iterator.  Changing one line:
typedef deque container; // changed!
container::iterator i;      // unaffected!
And suddenly, all the iterator code STILL works.
Not that I'm the first one to come up with this idea... it was adopted as 
standard in the STL.  :-)  Except that they use templates... :-)
There's also the new bit here - namespaces.  Everything in the STL is 
supposed to be in the "std" namespace.  So...
typedef std::deque container;
container::iterator i; // really a std::deque::iterator!  :-)
Again, changing the typedef to vector or list will 
require nearly zero change to the rest of your code.  :-)
--- Maximus/2 3.01
---------------
* Origin: Tanktalus' Tower BBS (PVT) (1:342/708)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.