Question:
I was trying to test a base class and I came across an unusual question: in the below code, why doesn’t the compiler complain about the return code type being a private member?
class TestCompile{ private: enum action { encrypt, decrypt }; public: action getAction(){return encrypt;}};
I believe it should complain, or at least warn the developer of the situation. I am using HPUX and aCC.
Answer:
Your code is valid. Remember that getAction() is a member of TestCompile, and as such, it’s allowed to access any member of its class, including returning a member’s value. The following program therefore is perfectly legal:
int main(){ TestCompile tc; tc.getAction(); //ok}
One caveat here, though: you can’t really do anything with getAction outside its class. It’s impossible to declare a variable of type TestCompile::action to store the result:
TestCompile::action a; //error; type action inacessible a = tc.getAction(); //error
A nitpicky compiler might issue a warning message in this case, but it’s not required to.