A member function can always access the data in __________ , (in C++).
2017
A member function can always access the data in __________ , (in C++).
- A.
the class of which it is member
- B.
the object of which it is a member
- C.
the public part of its class
- D.
the private part of its class
Attempted by 407 students.
Show answer & explanation
Correct answer: B
Answer: the object of which it is a member
Why this is correct: Non-static member functions are invoked for a particular object and have an implicit this pointer that refers to that object, so they can access the object's data directly.
Access levels: A member function of a class can access public, protected, and private members of the object it is called on (private and protected access applies to other instances of the same class as well).
Exception - static member functions: Static member functions belong to the class rather than any object, so they do not have an implicit this pointer and cannot access instance (non-static) data unless provided an object or pointer explicitly.
Quick summary: Non-static member functions operate on a specific object (the object of which they are a member) and can access all of that object's members; static member functions are class-level and cannot access per-object data without an object.