Referencing embedded enums in a class' implementation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arnaudk
    Contributor
    • Sep 2007
    • 425

    Referencing embedded enums in a class' implementation

    I have a class which contains an enum which I reference when I overload an operator for that class. However, it appears that I must prefix ClassName:: whenever I reference the enum:

    [code=cpp]
    // example.h
    class ExampleClass
    {
    public:
    enum WeekDay {SAT, SUN, MON, TUE, WED, THU, FRI};
    };

    // example.cpp
    #include "example.h"

    ostream& operator << (ostream& stm, ExampleClass const & ex)
    {
    WeekDay d = SAT; // error!
    ExampleClass::W eekDay d = ExampleClass::S AT; // works

    return stm;
    }
    [/code]

    Is there anyway to avoid this so that I can use the shorter first syntax, like we do with the "using namespace std;" statement to avoid prefixing std:: everywhere (which won't work here because Example is not a namespace)
    Last edited by arnaudk; May 8 '08, 01:35 PM. Reason: Example provided previously was incorrect.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    The correct thing to do is use the scope resolution operator.

    The "using" keyword is to be avoided since it introduces ambiguity.

    This won't compile:
    [code=cpp]
    #include <iostream>
    using namespace std;
    void cout()
    {
    cout << "Hello" << endl;
    }
    }
    int main()
    {
    cout();
    }
    [/code]

    But this will:
    [code=cpp]
    #include <iostream>
    void cout()
    {
    std::cout << "Hello" << std::endl;
    }
    }
    int main()
    {
    cout();
    }
    [/code]

    Stroustrup didn't want the "using" keyword in C++ to begin with because of problems of scope resolution.

    Avoid all shortcuts in C++.

    Comment

    • arnaudk
      Contributor
      • Sep 2007
      • 425

      #3
      Hi, and thanks for the reply.

      Granted, the designers of c++ must have introduced :: for a good reason. However, if I have to write ExampleClass:: twenty times within the body of a function, it would be nice to have a shortcut, like I often locally
      [code=cpp]
      typedef string::size_ty pe size_t;
      [/code]
      when working with strings, for example, to avoid writing string::size_ty pe twenty times. If this is done locally, then there is little risk of name conflicts.

      So I guess this is not possible, then.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        An enum is a named integer value. The named integer value is Example::stuff.

        So type Example::stuff twenty times. Everyone else does.

        If I just see stuff in your function, then what is it? An enum? A global variable? A member variable?

        Maybe one guy uses xyz for Example::stuff and someone else uses pdq.

        I mean you could always:
        [code=cpp]
        int x = Example::stuff;
        [/code]

        Then someone else could use xyz for Example::stuff and a third person muight use pdq.

        These shortcuts are a very bad habit to develop because they introduce ambiguity of meaning plus there is no guarantee that everyone will use the same shortcut. I would concentrate on explicit coding where there is only one meaning.

        This is the same reason why naming conventions don't work.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          By the way, I hope you are not really using

          [code=arnaudk]
          typedef string::size_ty pe size_t;
          [/code]

          because you now have ambiguity between the real size_t and the string::size_ty pe.

          size_type in STL-speak is the size of the type that's in the container. Everyone expects to see that.

          Maybe some one else uses
          [code=cpp]
          typedef string::size_ty pe byte;
          [/code]

          Now you have a mess. I understand that you like to do this but I can guarantee that almost everyone else does not.

          It's better to insist on string::size_ty pe and when this is not used, then fail the code in the code review and send the guy back to rerwrite it.

          Comment

          • arnaudk
            Contributor
            • Sep 2007
            • 425

            #6
            OK. Thanks for your comments which save me from learning the hard way! :-)

            Comment

            Working...