How can I access a variable that has been declared as static and protected in a class definition for use in another file b.cpp
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The only code allowed to use
protectedclass members (static or not) are explicit friends of the class in question and classes derived from the class in question (and of course, members of the class itself). Therefore, if “you” want to access that value, then “you” must either be a friend of that class or a member of a class derived from it.The protection classes (
public,protected, andprivate) exist to provide protection for data. By declaring the member to beprotected, the writer of that class is making a semi-strong statement about what code should be allowed to touch that piece of memory. If you’re not a derived class or have been given permission with an explicitfriendspecification, then you’re not allowed to touch it.You should not derive from a class solely to get access to a
protectedstatic member. You should only derive from a class if it makes sense to do so based on what your derived class is trying to do.