Simple questions for clarification

D

DGG

I have a maybe novice question to ask for expert advice.
1. How to define class-wide enums inside a class?
For example, currently I can think of a not satisfactory
implementation.
class MyWidget {
public:
const static int RELEASED = 0;
const static int ARMED = 1;
const static int PRESSED = 2;

const int option;
.....

public:
MyWidget() { state = RELEASED; }
public:
changeState(int newState);
setOption1() {option = 1;}
setOption2() {option = 2;}

private:
int state;
}

main()
{
MyWidget w1;

.....
w1.changeState(MyWidget::ARMED);
....
w1.setOption2();
}

But what I would like is to define a enum TYPE that is "inside"
the MyWidget class, so I can change the changeState() function's
signature to something like changeState(MyWidget::WidgetState
newState). Is this a better way? and How can I do this?


2. The usage of "const".
Again with the same example, I want to set the instance variable
"option" only once for each instance. Does defining it as const help?
and will it work? Ie., I don't know whether setOption1() or
setOption2() will be called, just content that only the first setting
is observed throught the life of one instance.

Thanks for any help
 
I

Ivan Vecerina

DGG said:
I have a maybe novice question to ask for expert advice.
1. How to define class-wide enums inside a class?
For example, currently I can think of a not satisfactory
implementation.
class MyWidget {
public:
const static int RELEASED = 0;
const static int ARMED = 1;
const static int PRESSED = 2; ....
changeState(int newState); .....
But what I would like is to define a enum TYPE that is "inside"
the MyWidget class, so I can change the changeState() function's
signature to something like changeState(MyWidget::WidgetState
newState). Is this a better way? and How can I do this?

Can't you just use an enum as follows:

class MyWidget {
public:
enum WidgetState { RELEASED, ARMED, PRESSED };

changeState(MyWidget::WidgetState newState);
.....


2. The usage of "const".
Again with the same example, I want to set the instance variable
"option" only once for each instance. Does defining it as const help?
and will it work? Ie., I don't know whether setOption1() or
setOption2() will be called, just content that only the first setting
is observed throught the life of one instance.
You can make it a const member, but then its value must be assigned
in the constructor of MyWidget.

class MyWidget {
public:
....

MyWidget(int optionValue) : option(optionValue) {}
....
private:
int const option;

If the 'option' value cannot be passed at construction time,
the best you can do is use a private data member initialized
to a default/invalid value. A 'setter' function can then only
check and generate a run-time error it is gets called twice.



I hope this helps,
Ivan
 
I

int i=0

Il 2004-12-17, Ivan Vecerina
class MyWidget {
public:
enum WidgetState { RELEASED, ARMED, PRESSED };

changeState(MyWidget::WidgetState newState);
....

Yes, but it will be possible to call

the_widget.changeState( 1 ); // not so clear ...

I think would be better

class MyWidget {
public:
typedef enum { RELEASED, ARMED, PRESSED } WidgetState;

changeState(WidgetState newState);
....

No more possible to use ints

the_widget.changeState(MyWidget::RELEASED);

but

the_widget.changeState( 1 ); // <-- ERROR: cannot convert int to ...
 
I

Ivan Vecerina

int i=0 0 said:
Il 2004-12-17, Ivan Vecerina


Yes, but it will be possible to call

the_widget.changeState( 1 ); // not so clear ...

No: this will actually trigger a compile error.
I think would be better

class MyWidget {
public:
typedef enum { RELEASED, ARMED, PRESSED } WidgetState;

changeState(WidgetState newState);

In ISO C++, AFAICT, this is totally equivalent to the
previous example.


Cheers,
Ivan
 
D

DGG

Thanks for the response.

I think the "const" member would be more useful if it could be set in
any member function, as long as the second setting will trigger a
run-time exception.

If "const" can only be set inside a constructor, its use is restricted
to setting initial configuration options. By asking the original
question, I had in mind what more use can a const member have.
 
J

Jonathan Mcdougall

DGG said:
Thanks for the response.

I think the "const" member would be more useful if it could be set in
any member function, as long as the second setting will trigger a
run-time exception.

If "const" can only be set inside a constructor, its use is restricted
to setting initial configuration options. By asking the original
question, I had in mind what more use can a const member have.

A const member needs to have a value from start :

int maint()
{
const int ci; // error

const int ci = 2; // ok
}

That's the same thing for classes: const members must be initialized in
constructors. If it must be given a value elsewhere, it is *not* a
constant member (that is, its value will change over time: first in the
ctor and then in another function).

What you need is care, not const. That's what encapsulation is for.


Jonathan
 

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,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top