Simple questions for clarification

Discussion in 'C++' started by DGG, Dec 17, 2004.

  1. DGG

    DGG Guest

    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
     
    DGG, Dec 17, 2004
    #1
    1. Advertising

  2. "DGG" <> wrote in message
    news:...
    >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
     
    Ivan Vecerina, Dec 17, 2004
    #2
    1. Advertising

  3. DGG

    int i=0 Guest

    Il 2004-12-17, Ivan Vecerina
    <> ha scritto:
    > "DGG" <> wrote in message
    > news:...
    > 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
     
    int i=0, Dec 17, 2004
    #3
  4. "int i=0 0" <'@.'> wrote in message
    news:ADBwd.46669$...
    > Il 2004-12-17, Ivan Vecerina
    > <> ha scritto:
    >> "DGG" <> wrote in message
    >> news:...
    >> 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
     
    Ivan Vecerina, Dec 17, 2004
    #4
  5. DGG

    DGG Guest

    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.
     
    DGG, Dec 20, 2004
    #5
  6. 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
     
    Jonathan Mcdougall, Dec 20, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nikos Mitas
    Replies:
    2
    Views:
    1,058
    Hubble
    Sep 27, 2005
  2. ss

    One Clarification

    ss, Aug 19, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    319
    Kevin Spencer
    Aug 19, 2003
  3. Ken Cox [Microsoft MVP]

    Database Connection clarification

    Ken Cox [Microsoft MVP], Nov 26, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    350
    Jon Booth
    Nov 26, 2003
  4. Alex Agranov

    Tabstrip clarification

    Alex Agranov, Jan 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    318
    Alex Agranov
    Jan 20, 2004
  5. Lerp

    Clarification Needed

    Lerp, Aug 17, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    344
    Edd Connolly
    Aug 18, 2004
Loading...

Share This Page