enum ErrType
{
SUCCESS,
FAILURE,
WARNING
};
class ErrMsg
{
ErrType code;
String info;
};
ErrMsg bar()
{
ErrMsg fail;
fail.code = FAILURE;
fail.info = "Function failed. :(";
return fail;
}
bool foo()
{
if (bar()) //evaluates to false if code != SUCCESS
cout << "Failed!";
else cout << "Success!";
}
Auto-cast a class in c++
I'm working on an error message class, and I want to be able to check whether it was an error or not as easily as if I was checking whether a boolean was true or false. Here is an example of what I want to do:
The output should be "Failed!"
Basically, I need some way to case an ErrMsg to a boolean. How would I do this?
You can use operator overloading for casting.
Note that operator bool doesn't have a return type, since it's implied.
#include <iostream>using std::cout;class EvenNumber{public: EvenNumber(int i) : num(i) { } operator bool() { return ((num % 2) == 0); } int num;};int main (int argc, char** args){ EvenNumber x5(5); EvenNumber x6(6); if (x5) cout << "5 is even."; if (x6) cout << "6 is even.";return 1;}
Note that operator bool doesn't have a return type, since it's implied.
If ErrMsg has a function called operator bool() then your code should work.
But this implicit casting is frowned upon, and it would be prefereable to imlemant operator!() instead, especially as it makes more sense to most coders as when they see if( !someCondition ) to be a failure condition rather than if( condition )
So:
Finally, if you do that you end up not saving the ErrMsg instance returned from the function, which prevents you from doing something useful with the .code and .info members.
So you could just use an exception instead:
But this implicit casting is frowned upon, and it would be prefereable to imlemant operator!() instead, especially as it makes more sense to most coders as when they see if( !someCondition ) to be a failure condition rather than if( condition )
So:
enum ErrType{ SUCCESS, FAILURE, WARNING};class ErrMsg{ ErrType code; String info; bool operator!() const { return code != SUCCESS; }};ErrMsg bar(){ ErrMsg fail; fail.code = FAILURE; fail.info = "Function failed. :("; return fail;}bool foo(){ if (!bar()) //evaluates to false if code != SUCCESS cout << "Failed!"; else cout << "Success!";}
Finally, if you do that you end up not saving the ErrMsg instance returned from the function, which prevents you from doing something useful with the .code and .info members.
So you could just use an exception instead:
class Exception : public std::exception{ std::string info; Exception( const std::string &info_ ) : info(info_){} virtual const char *what() const { return info.c_str(); }};class SomeException : public Exception{...}class SomeOtherException : public Exception{...}void bar(){ if( rand() % 2 ) throw SomeException("I always fail badly"); else throw SomeOtherException ("Except when I fail reeeally badly");}bool foo(){ try { bar(); } catch( SomeException &e ) { cerr << e.what(); } // for some reason we cant deal with SomeOtherExceptions, but maybe our caller can...}
first of all, if you don't use an access specifier on class members, they will be private by default which means you won't be able to access the members outside the class.
I'd suggest using accessors methods or make them public like this:
Second, you can write your foo() function like this:
but you won't be able to access the 'info' member of the ErrMsg class.
So I'd suggest using:
Hope that helps !
I'd suggest using accessors methods or make them public like this:
class ErrMsg{public: ErrType code; String info; };
Second, you can write your foo() function like this:
void foo(){ if( bar().code == FAILURE ) cout << "Failed!" << endl; else cout << "Success!" << endl;}
but you won't be able to access the 'info' member of the ErrMsg class.
So I'd suggest using:
void foo(){ ErrMsg err = bar(); if( err.code == FAILURE ) cout << "Failed!: " << err.info << endl; else cout << "Success!" << endl;}
Hope that helps !
@lemurion: You do not want data accessable to everyone. That totally defeats the whole OO idea. Use, atleast, Get/Set functions to access the data.
Quote:Original post by Anonymous Poster
@lemurion: You do not want data accessable to everyone. That totally defeats the whole OO idea. Use, atleast, Get/Set functions to access the data.
That dosn't do much for "the whole OO idea" (who's definition, might I ask?) - and for a thin data-grouping wrapper like this there's really not that much benifit to privatizing that data.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement