Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
C++: class with enum & function taking it, best way to call?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
Akkara
Bodhisattva
Bodhisattva


Joined: 28 Mar 2006
Posts: 6702
Location: &akkara

PostPosted: Sun May 01, 2016 8:36 am    Post subject: C++: class with enum & function taking it, best way to c Reply with quote

I'm thinking what's the best way to do error-code returns in C++.

I have a error class that wraps an error-code integer and provides a nicer and hopefully clearer and easier to use interface. Something like this:
Code:
class ErrorCode {
    public:
        enum err_nums {
            e_eof = 1,
            e_no_header = 2,
            e_no_codec = 4,
            e_range = 8,
            ....
        };

        ErrorCode() : errnum(0) {}
        ErrorCode &raise(enum err_nums e) { errnum |= e; }
        /* ... other methods omitted from this example... */

    private:
        unsigned errnum;
};

This generally works (except for the fact I abbreviated the example for clarity, so I don't think this particular one will compile as-is).

However I don't like how the calls are done:
Code:
ErrorCode some_work_function(...)
{
    ErrorCode err;

    if(bad_thing) {
        err.raise(ErrorCode::e_no_header);  //  <== Ugly line
    }
    ...
    return err;
}

The err.raise function only takes a very specific enum as an argument. It would be nice if I didn't have to write "ErrorCode::" all over the place each time I invoke it.

I tried a few alternatives, but they all seem to have their problems:
  • Declare the enum globally:
      Pollutes the namespace for something that's quite specific to this class


  • Declare a second class, ErrorNum, have it hold the enum values. Have ErrorCode publicly inherit from it. And other classes with functions that need access to then privately inherits from ErrorNum:
      Seems like a hack. Is inheritance meant to be used this way?


  • Define a family of functions, one for each error value, something like raise_e_no_header():
      Duplicates a lot of code, becomes unwieldy as the number of possible errors goes up (I'm currently at around 30).

There's probably a "good" way to do this, but I don't know the fine points of C++ well enough to know what it is. Please enlighten me.

Thanks!
_________________
Many think that Dilbert is a comic. Unfortunately it is a documentary.
Back to top
View user's profile Send private message
Genone
Retired Dev
Retired Dev


Joined: 14 Mar 2003
Posts: 9523
Location: beyond the rim

PostPosted: Mon May 02, 2016 9:01 am    Post subject: Reply with quote

Well, passing the class namespace qualifier is pretty standard practice.
If you really want to avoid it you could define a global enum encapsulated in a special namespace and import the identifiers from that namespace with 'using ...' if the class qualifier really disturbs you that much.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming 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