Problem with declaration of enum

Y

yatko

Hi all;

I have problem about declaration of enum in a class. I want to write
something like that; but it gives error.


class Foo
{
public:
Foo( RunningMode); // compiler gives "RunningMode has
not been declared" error at this point.

private:
enum RunningMode {
BLOCKING,
TIMED_WAIT,
NONBLOCKING
};

const RunningMode mode;
};

When I write the declaration of RunningMode before Foo( RunningMode),
it is ok.

class Foo
{
public:
enum RunningMode {
BLOCKING,
TIMED_WAIT,
NONBLOCKING
};

Foo( RunningMode);

private:
const RunningMode mode;
};

But at this time enum RunningMode is a public declaration, and I want
to declare it as private. How should I write the class ?

Thanks for any help

yatko
 
A

Alf P. Steinbach

* yatko:
I have problem about declaration of enum in a class. I want to write
something like that; but it gives error.


class Foo
{
public:
Foo( RunningMode); // compiler gives "RunningMode has
not been declared" error at this point.

private:
enum RunningMode {
BLOCKING,
TIMED_WAIT,
NONBLOCKING
};

const RunningMode mode;
};

When I write the declaration of RunningMode before Foo( RunningMode),
it is ok.

Yes, this can be a little bit confusing, because it /seems/ as if you
can use some things that are declared later in a class, e.g.

struct S
{
void x() { y(); }
void y() {}
};

But this definition is treated as if it were written as

struct S
{
void x();
void y();
};

inline void S::x() { bar(); }
inline void S::y() {}

and as you can see, there's really no forward referencing involved.

Your example does, however, involve forward referencing, using something
that hasn't yet been declared:

struct Foo
{
Foo( RunningMode ); // RunningMode used before it's declared.
enum RunningMode { a, b, c };
};

class Foo
{
public:
enum RunningMode {
BLOCKING,
TIMED_WAIT,
NONBLOCKING
};

Preferentially reserve ALL UPPERCASE names for macros: using them for
constants is a Java'ism, and is a bit dangerous in C++.

Foo( RunningMode);

private:
const RunningMode mode;
};

But at this time enum RunningMode is a public declaration, and I want
to declare it as private. How should I write the class ?

Technically, in order to declare it private, just declare it private:

class Foo
{
private:
enum RunningMode { a, b, c };

public:
Foo( RunningMode );

RunningMode const mode;
};

However, first, client code will not be able to invoke the constructor,
because the client code won't have access to the RunningMode type.

You can resolve that in various ways, e.g. by making RunningMode public,
or by providing a factory function.

Second, the const member means that Foo objects can't be copied by
assignment, and so to avoid silly-warnings you should declare a private
copy assignment operator. Presumably you'll also want to declare a
private copy constructor.


Cheers, & hth.,

- Alf
 
Y

yatko

Hi Alf;

Thanks for your suggestions, and now I know what I should do clearly.
Preferentially reserve ALL UPPERCASE names for macros: using them for
constants is a Java'ism, and is a bit dangerous in C++.

And one more thing I don't have enough programming experience about c+
+ coding style and conventions. Could you suggest me any tutorials,
ebooks for C++ coding conventions?

Bests,

yatko
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top