Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Simple questions for clarification

Reply
Thread Tools

Simple questions for clarification

 
 
DGG
Guest
Posts: n/a
 
      12-17-2004
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

 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      12-17-2004
"DGG" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form


 
Reply With Quote
 
 
 
 
int i=0
Guest
Posts: n/a
 
      12-17-2004
Il 2004-12-17, Ivan Vecerina
<(E-Mail Removed) > ha scritto:
> "DGG" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.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
 
Reply With Quote
 
Ivan Vecerina
Guest
Posts: n/a
 
      12-17-2004
"int i=0 0" <'@.'> wrote in message
news:ADBwd.46669$(E-Mail Removed)...
> Il 2004-12-17, Ivan Vecerina
> <(E-Mail Removed) > ha scritto:
>> "DGG" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) oups.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.

> 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


 
Reply With Quote
 
DGG
Guest
Posts: n/a
 
      12-20-2004
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.

 
Reply With Quote
 
Jonathan Mcdougall
Guest
Posts: n/a
 
      12-20-2004
DGG wrote:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
35mm slides scanner clarification questions All Things Mopar Digital Photography 115 02-24-2006 05:06 PM
Clarification Term: "Behavioural Description" Nikos Mitas VHDL 2 09-27-2005 05:36 AM
Re: Questions....questions....questions Patrick Michael A+ Certification 0 06-16-2004 04:53 PM
clarification =?Utf-8?B?c3JpbmF0aGU=?= Microsoft Certification 0 05-22-2004 11:06 AM
Cisco PIX authentication proxy clarification lombardi Cisco 3 04-03-2004 11:13 PM



Advertisments