devxlogo

Private Member as Return Type?

Private Member as Return Type?

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.

See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist