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.

Share the Post:
data observability

Data Observability Explained

Data is the lifeblood of any successful business, as it is the driving force behind critical decision-making, insight generation, and strategic development. However, due to its intricate nature, ensuring the

Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular