473,320 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Simple questions for clarification

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

Jul 22 '05 #1
5 1086
"DGG" <to******@nectech.co.uk> wrote in message
news:11**********************@f14g2000cwb.googlegr 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
Jul 22 '05 #2
Il 2004-12-17, Ivan Vecerina
<NO**********************************@vecerina.com > ha scritto:
"DGG" <to******@nectech.co.uk> wrote in message
news:11**********************@f14g2000cwb.googlegr 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
Jul 22 '05 #3
"int i=0 0" <'@.'> wrote in message
news:AD******************@tornado.fastwebnet.it...
Il 2004-12-17, Ivan Vecerina
<NO**********************************@vecerina.com > ha scritto:
"DGG" <to******@nectech.co.uk> wrote in message
news:11**********************@f14g2000cwb.googlegr 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
Jul 22 '05 #4
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.

Jul 22 '05 #5
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
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Smutny30 | last post by:
Hello, I consider partitioning a huge table. I am not sure wheather it is possioble only in partitioned databases. I found somewhere in docs ( http://tinyurl.com/4oara ) that there are (simple,...
15
by: S.Tobias | last post by:
In the past weeks I asked two questions concerning unions, and I got not a single reply to either. What should I do next? Re-post them? Ask them again in csc? -- Stan Tobias sed 's///g' to...
8
by: Sai Kit Tong | last post by:
In the article, the description for "Modiy DLL That Contains Consumers That Use Managed Code and DLL Exports or Managed Entry Points" suggests the creation of the class ManagedWrapper. If I...
3
by: djc | last post by:
I have a couple questions about authentication and authorization in asp.net 2.0. 1) I see there are still the same authentication mode options as in 1.1 (windows, forms, or passport). However in...
7
by: javedna | last post by:
Hi guys Ive got a simple problem, im designing an online questionnaire and on submission the coding that I have used to validate whether a user has filled in all the questions is supposed to...
102
by: dreamznatcher | last post by:
Hello, I'm considering a career switch to a more database-related job, but need help on a few questions and issues. I'm a Computer Engineering graduate and have always felt most comfortable...
30
by: GeorgeRXZ | last post by:
Hi Friends, I have some questions related to C Language. 1What is the difference between the standard C language and Non standard C language ? 2which is better C Lanugage, C under Linux/...
11
by: Stef Mientki | last post by:
hello, I need to translate the following string a = '(0, 0, 0, 255), (192, 192, 192, 255), True, 8' into the following list or tuple b = Is there a simple way to to this. (Not needed...
4
by: Jeff | last post by:
I haven't used php to send mail yet and have some questions. I see that you can add headers, and these days it is essential to have a real "from". But I see you can also do this: ...
17
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /*...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.