home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!newsgate.watson.ibm.com!news.ans.net!cmcl2!HARRISON.CS.NYU.EDU!osinski
- From: osinski@HARRISON.CS.NYU.EDU (Ed Osinski)
- Subject: Re: 'virtual' static members
- Message-ID: <9208132115.AA24368@HARRISON.CS.NYU.EDU>
- Sender: daemon@cmcl2.nyu.edu (Mr Background)
- Reply-To: osinski@cs.nyu.edu
- Organization: New York University
- References: <1992Aug12.201004.1257@CSD-NewsHost.Stanford.EDU>
- Date: Thu, 13 Aug 1992 21:15:47 GMT
- Lines: 63
-
- In article <1992Aug12.201004.1257@CSD-NewsHost.Stanford.EDU>, ralph@Xenon.Stanford.EDU (Ralph Melton) writes:
- |>
- |> I am trying to write a general enumeration class that can do output
- |> of itself. If I was doing it as a single class, part of it might
- |> look like
- |>
- |> class myEnum
- |> {
- |> int value;
- |> static int names;
- |> friend ostream& operator<< (ostream&, myEnum&);
- |> //...
- |> }
- |>
- |> ostream& operator<< (ostream& os, myEnum& e);
- |> { return ( os << e.names[e.value] ); }
- |>
- |>
- |> This is all well and good, but I would like to make this into an
- |> abstract base class, so that for a specific type of MyEnum, you subclass
- |> myEnum, and give the subclass its own static "names" array.
- |> I would like to leave the operator<< function the same, if possible.
- |>
- |> Ideally, I would like to be able to define a fully functional
- |> subclass of myEnum in just one or two lines.
- |>
- |> Can this be done? If I don't declare the array in myEnum, this
- |> operator<< function can't see it; if I do declare it, I can't
- |> override it in subclasses.
- |>
- |> I think I can see more or less skanky ways around this problem using
- |> macros or specialized constructors; I am curious as to whether there
- |> is any way to declare a static member that is different for every
- |> subclass.
-
- No there isn't, but you can get the same effect by declaring static data
- members in each class to hold the table, and defining a virtual member function
- that returns that class's table. In your print function, instead of referring
- to "names", you would refer to "enum_obj.get_names()". This will require each
- class to have:
-
- - a new static data member for the table (the variable name can be
- the same in each class)
- - an initialization of that static data member (which must be done
- outside the class)
- - a redefinition of the "get_names" function which simply returns the
- static data member specifying the table of that class
-
- |>
- |>
- |> Ralph Melton
- |> --
- |> Ralph Melton ralph@cs.stanford.edu
- |> "Then ye shall know the truth, and the truth shall make you free."
-
- -----------------------------------------------------------------------
- Ed Osinski |
- Computer Science Department |
- New York University | This space for rent
- E-mail: osinski@cs.nyu.edu |
- Voice: (212) 998-3515 |
- Fax: (212) 995-4123 |
- -----------------------------------------------------------------------
-