Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: C++
Expertise: Beginner
May 16, 2000



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Private Member as Return Type?

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
      enum action { encrypt, decrypt };
      action getAction(){return encrypt;}
I believe it should complain, or at least warn the developer of the situation. I am using HPUX and aCC.

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.
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date