dcsimg
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


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

WEBINAR:

On-Demand

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


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

 

 

 

 

 


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

 

 

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