Topic: Enum as a member?


Author: Charles Miller <cmiller@oasystel.com>
Date: Tue, 3 Apr 2001 01:42:33 CST
Raw View
In a previous article, David Sachs states...
> enums can be converted to integer types. Since there is no overriding
> operator<< your program will output the integer value of RED, which is
> normally 0.

Yes, but I thought the dot operator allowed access to an actual MEMBER
VARIABLE.  Since Foo does not contain any data members--in particular any
Color members--my intuition made me believe something was amiss.

Ron Natalie, in a previous response wrote:
> 7.2/10  The enum-name and each enumerator declared by an
> enum-specifier is declared in the scope that immediately
> contains the enum-specifier. These names obey the scope
> rules defined for all names in (3.3) and
> (3.4). An enumerator declared in class scope can be referred
> to using the class member access operators
> (::, . (dot) and -> (arrow)), see 5.2.5.

5.2.5 seems problematic as well.  Even though the enumeration Color is
declared within class Foo's scope, and is a member of Foo, there is no
Color variable in the instance f of Foo.  Obviously, I do not understand
completely the behavior of dot operator.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Charles Miller <cmiller@oasystel.com>
Date: Sun, 1 Apr 2001 02:43:06 GMT
Raw View
A colleague wrote the following code, which compiled and executed:

class Foo
{
public:
    enum Color { RED, GREEN, BLUE };
    Foo() {}
};

void main()
{
   Foo f;
   cout << f.RED << endl;
}

The output was:
0

I would have thought only Foo::RED would be appropriate.

Question: What is the language basis for this behavior (obtaining the
value of f.RED)?  Foo does not have any Color member, and I believe an
enum value does not have actual storage in a class.

Regards,
Charles Miller

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Ron Natalie <ron@stop.mail-abuse.org>
Date: Sun, 1 Apr 2001 03:01:14 GMT
Raw View

Charles Miller wrote:
>

>
> Question: What is the language basis for this behavior (obtaining the
> value of f.RED)?  Foo does not have any Color member, and I believe an
> enum value does not have actual storage in a class.

7.2/10  The enum-name and each enumerator declared by an enum-specifier is declared in the scope that immediately contains the enum-specifier. These names obey the scope rules defined for all names
in (3.3) and
(3.4). An enumerator declared in class scope can be referred to using the class member access operators
(::, . (dot) and -> (arrow)), see 5.2.5.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "David Sachs" <sachs@fnal.gov>
Date: Sun, 1 Apr 2001 20:54:43 CST
Raw View
"Charles Miller" <cmiller@oasystel.com> wrote in message
news:MPG.152fce7dc1135411989687@news...
> A colleague wrote the following code, which compiled and executed:
>
> class Foo
> {
> public:
>     enum Color { RED, GREEN, BLUE };
>     Foo() {}
> };
>
> void main()
> {
>    Foo f;
>    cout << f.RED << endl;
> }
>
> The output was:
> 0
>
> I would have thought only Foo::RED would be appropriate.
>

enums can be converted to integer types. Since there is no overriding
operator<< your program will output the integer value of RED, which is
normally 0.




---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]