C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Simple questions for clarification

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
DGG
Guest





PostPosted: Fri Dec 17, 2004 11:14 am    Post subject: Simple questions for clarification Reply with quote



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

Back to top
Ivan Vecerina
Guest





PostPosted: Fri Dec 17, 2004 1:12 pm    Post subject: Re: Simple questions for clarification Reply with quote



"DGG" <tong.yin (AT) nectech (DOT) co.uk> wrote

Quote:
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);
.....



Quote:
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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form



Back to top
int i=0
Guest





PostPosted: Fri Dec 17, 2004 2:00 pm    Post subject: Re: Simple questions for clarification Reply with quote



Il 2004-12-17, Ivan Vecerina
<NOT_VALID_please_use_contact_webform (AT) vecerina (DOT) com> ha scritto:
Quote:
"DGG" <tong.yin (AT) nectech (DOT) co.uk> wrote in message
news:1103282085.280387.154110 (AT) f14g2000cwb (DOT) googlegroups.com...
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 ...

--
int

Back to top
Ivan Vecerina
Guest





PostPosted: Fri Dec 17, 2004 2:10 pm    Post subject: Re: Simple questions for clarification Reply with quote

"int i=0 0" <'@.'> wrote

Quote:
Il 2004-12-17, Ivan Vecerina
[email]NOT_VALID_please_use_contact_webform (AT) vecerina (DOT) com[/email]> ha scritto:
"DGG" <tong.yin (AT) nectech (DOT) co.uk> wrote in message
news:1103282085.280387.154110 (AT) f14g2000cwb (DOT) googlegroups.com...
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 ...

No: this will actually trigger a compile error.

Quote:
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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form



Back to top
DGG
Guest





PostPosted: Mon Dec 20, 2004 5:52 pm    Post subject: Re: Simple questions for clarification Reply with quote

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.

Back to top
Jonathan Mcdougall
Guest





PostPosted: Mon Dec 20, 2004 6:37 pm    Post subject: Re: Simple questions for clarification Reply with quote

DGG wrote:
Quote:
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

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.