Topic: Enums in classes
Author: wienczny@web.de (Stephan Wienczny)
Date: Sun, 29 Sep 2002 21:18:52 +0000 (UTC) Raw View
Hallo NG,
I've got an enum in one of my classes and to compiler trying to compile it:
class Foo
{
public:
int dofoo();
enum TestEnum{
Elem1,
Elem2
}
};
int FOO::dofoo()
{
// Here comes my problem:
// Do I have to write
some_var = FOO::TestEnum::Elem1;
// or
some_var = FOO::Elem1;
// to acces my enum?
}
The first example makes IMHO more sense than the other, but does only
work on MSVC. I had to use the second example to compile it using gcc
(version 3.2). Which one is the standard way?
Cu Stephan
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]
Author: vAbazarov@dAnai.com ("Victor Bazarov")
Date: Sun, 29 Sep 2002 21:56:30 +0000 (UTC) Raw View
"Stephan Wienczny" <wienczny@web.de> wrote...
> Hallo NG,
>
> I've got an enum in one of my classes and to compiler trying to compile
it:
>
> class Foo
> {
> public:
> int dofoo();
>
> enum TestEnum{
> Elem1,
> Elem2
> }
^
A semicolon is missing here, I believe.
> };
>
> int FOO::dofoo()
> {
> // Here comes my problem:
> // Do I have to write
> some_var = FOO::TestEnum::Elem1;
> // or
> some_var = FOO::Elem1;
> // to acces my enum?
> }
>
> The first example makes IMHO more sense than the other, but does only
> work on MSVC. I had to use the second example to compile it using gcc
> (version 3.2). Which one is the standard way?
You can drop both prefixes. "Elem1" or "Elem2" are accessible
within the class scope (which also includes the scope of member
functions):
int FOO::dofoo()
{
TestEnum some_var = Elem1;
return 42;
}
(Standard, 9.2/1: "The enumerators of an enumeration (7.2) defined
in the class are members of the class.", and 3.3.6/1 "The potential
scope of a name declared in a class consists not only of the
declarative region following the name's declarator, but also of all
function bodies, default arguments, and constructor ctor-initializers
in that class (including such things in nested classes)." )
Victor
--
Please remove capital A's from my address when replying by mail
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]