[comp.lang.c++] Private Enumerations?

minar@reed.bitnet (Nelson Minar,L08,x640,7776519) (09/05/90)

I'm fairly new to C++, so forgive me if this question is malformed.

Is there a way, or should there be a way, to allow enumerations that have scope
only within their class?

For example:

class Boolean {
public:
  enum boolEnum { false, true };
  boolEnum value;
}

so then I could say

main() {
  Boolean bool;
  int foo;
  bool.value = true;
  foo = true;				// ILLEGAL - out of scope
}

currently I am declaring the enumeration to have global scope, which is
displeasing: it would allow foo to be assigned as mentioned.

sarima@tdatirv.UUCP (Stanley Friesen) (09/07/90)

In article <15414@reed.UUCP> minar@reed.bitnet (Nelson Minar) writes:
>Is there a way, or should there be a way, to allow enumerations that have scope
>only within their class?
 
>For example:
 
>class Boolean {
>public:
>  enum boolEnum { false, true };
>  boolEnum value;
>}

This is indeed quite legal, at least in version 2.x.  And it does declare an
enumeration that is a member of the class Boolean.

>so then I could say
 
>main() {
>  Boolean bool;
>  int foo;
>  bool.value = true;
>  foo = true;				// ILLEGAL - out of scope
>}

	Unfortunately, this is not how it works.  As you have Boolean declared
both assignments are 'illegal', and for the same reason - there is no
identifier 'true' in scope in main().  The identifier 'true' is only in scope
*within* the class Boolean, that is from member functions of Boolean or by
using the scope resolution operator.  Thus both of the following are legal:

bool.value = Boolean::true;
foo = Boolean::true;

This is so because the enum is declared as public, making it publicly
accessible.  I know of no way to use this feature to get what you want,
if you declare the enum private it is only usable in member and friend
functions, making its use in main() illegal.
-----------------
uunet!tdatirv!sarima				(Stanley Friesen)

jk@cs.man.ac.uk (John Kewley ICL) (09/07/90)

The following is not particularly useful, does not involve private enumerations,
yet could be of some interest:

// Boolean.hxx

#ifndef BOOLEAN_H
#define BOOLEAN_H

class Boolean
{
public:
    Boolean();
    Boolean(int n);

private:
    int value;
};

extern const Boolean true;
extern const Boolean false;
#endif /* BOOLEAN_H */
---------------------------------------------
// Boolean.cxx
#include "Boolean.hxx"

#define FALSE 0
#define TRUE 1

const Boolean true= FALSE;
const Boolean false= TRUE;

Boolean::Boolean()
{
        value= FALSE;
}
Boolean::Boolean(int n)
{
        value= (!n)?FALSE:TRUE;
}
---------------------------------------------
// use.cxx
#include "Boolean.hxx"

main()
{
    Boolean bool;
    int foo;
    bool = true;
    foo = true;
};

--
        J.K.
 
John M. Kewley, ICL, Wenlock Way, West Gorton, Manchester. M12 5DR
Tel:   (+44) 61 223 1301 X2138  Email: jk@r6.cs.man.ac.uk / jk@nw.stl.stc.co.uk

johnb@srchtec.UUCP (John Baldwin) (09/11/90)

In article <1683@m1.cs.man.ac.uk> jk@cs.man.ac.uk (John Kewley ICL) writes:
>
>class Boolean
>{
>public:
>    Boolean();
>    Boolean(int n);
>
>private:
>    int value;
>};

Okay, now add...

Boolean&  Boolean::operator=(int rhv)   // rhv == right-hand value
{
   value = rhv ? TRUE : FALSE;
}


   Now you can't even manage to assign a value other than TRUE
or FALSE to the class instance.  With a little more work on top
of this, its a true multipurpose boolean.
-- 
John T. Baldwin                      |  johnb%srchtec.uucp@mathcs.emory.edu
Search Technology, Inc.              | 
                                     | "... I had an infinite loop,
My opinions; not my employers'.      |  but it was only for a little while..."