Topic: static classes : is it possible ???


Author: fisherj@ucs.orst.edu (Joseph A. Fisher)
Date: 31 Mar 92 00:26:22 GMT
Raw View
Static classes ???

   Is it possible to have a static class. I have a class with much data
   and instead of using:
                         static int intVar;
                         static float floatVar;

   I would like to do something like:
                         static class BaseClass
                            {
                            protected:
                               int intVar;
                               float floatVar;

                            public:
                             ...
                            };
   I would hope that doing this would make all the data in the class
   static. Can this be done and if so, does it have any side effects?
--
Joe Fisher
fisherj@ucs.orst.edu
Oregon State University




Author: eric@tfs.com (Eric Smith)
Date: 31 Mar 92 13:16:29 GMT
Raw View
In article <1992Mar31.002622.3075@talon.ucs.orst.edu> fisherj@ucs.orst.edu (Joseph A. Fisher) writes:
>Static classes ???
>
>   Is it possible to have a static class. I have a class with much data
>   and instead of using:
>                         static int intVar;
>                         static float floatVar;
>
>   I would like to do something like:
>                         static class BaseClass
>                            {
>                            protected:
>                               int intVar;
>                               float floatVar;
>
>                            public:
>                             ...
>                            };
>   I would hope that doing this would make all the data in the class
>   static. Can this be done and if so, does it have any side effects?
>--
>Joe Fisher
>fisherj@ucs.orst.edu
>Oregon State University


That would conflict with existing syntax.  You can declare class
objects in the same declaration with the class.  If your feature
was implemented, how would the compiler know in such cases whether
you wanted the class members or the class objects to be static?

For example:

                         static class BaseClass {
                            protected:
                               int intVar;
                               float floatVar;
                            public:
                             ...
                            } a,b,c;

will declare objects a, b, and c, of your class, and make all
three of them static.

A much better approach would be to just put the keyword "static"
before each member of the class.  That takes some extra text but
saves adding more syntax to an already overloaded language.




Author: landauer@morocco.Eng.Sun.COM (-8 Doug Landauer 8-)
Date: 31 Mar 92 18:10:05 GMT
Raw View
Joe Fisher asks:
> >   I would like to do something like:
> >  static class BaseClass { /* ... */ };
> >   I would hope that doing this would make all the data in the class
> >   static. Can this be done ?

Almost.  Just write it like this:
 static class BaseClass { /* ... */ } Joes_Static_Class_Dummy;

> >   and if so, does it have any side effects?

Well, yeah -- there will be one new static variable, named
"Joes_Static_Class_Dummy".  Other than that, the class should behave just
as you desire.  For clarity (and because people are more used to seeing it
done this way), you might prefer to write it like this:

 class BaseClass { /* ... */ };
 static BaseClass Joes_Static_Class_Dummy;

Eric Smith replies:
> A much better approach would be to just put the keyword "static"
> before each member of the class.  That takes some extra text but
> saves adding more syntax to an already overloaded language.

Just creating a singleton dummy object is somewhat more flexible --
it makes it easier when you change your mind later and want the
class to be a normal class after all.
--
Doug Landauer -- landauer@eng.sun.com
SMI[STE]->SunPro::Languages.PE(C++);




Author: landauer@morocco.Eng.Sun.COM (-8 Doug Landauer 8-)
Date: 1 Apr 92 03:12:52 GMT
Raw View
Joe Fisher asks about a static class ...
I reply suggesting a singleton, static object ...
JF>   and if so, does it have any side effects?
DL> Well, yeah -- there will be one new static variable, ...

Oh yeah, there's one other side effect -- the class members will no longer
be static if you derive from the class in question.  I guess I should have
looked more closely at the name of your class ("BaseClass") to notice that
you wanted to derive from it.  So Eric was right:

ES> A much better approach would be to just put the keyword "static"
ES> before each member of the class.  That takes some extra text but
ES> saves adding more syntax to an already overloaded language.

DL> Just creating a singleton dummy object is somewhat more flexible --
DL> it makes it easier when you change your mind later and want the
DL> class to be a normal class after all.

Just creating a singleton dummy object is also somewhat less flexible --
if you want other classes derived from this guy.
--
Doug Landauer -- landauer@eng.sun.com