Dot operator (.) in C++: which pair of entities, read from left to right, can it legally connect? Pick the most precise description of member access.

Difficulty: Easy

Correct Answer: A class object and a member of that class.

Explanation:


Introduction / Context:
The dot operator is the standard member-access operator for objects in C++. Correctly identifying what appears on each side clarifies how to read and write idiomatic code.



Given Data / Assumptions:

  • The left operand must be an object, reference, or lvalue of class/struct/union type.
  • The right operand must be a member (data or function) of that type.
  • Static vs non-static members both use the same syntax on objects; scope operator :: is for types/namespaces.



Concept / Approach:
Member access uses object.member. For pointer-to-object, use the arrow operator: ptr->member. To access static members without an instance, qualified lookup uses ClassName::member. Thus, dot specifically connects an object on the left to a member of that object’s class on the right.



Step-by-Step Solution:
1) Point p; p.x; → object p to its data member x.2) p.length(); → object p to its member function length().3) Point::origin(); → uses scope operator, not dot (class to static member).4) Point* q; q->x; → uses arrow, not dot.



Verification / Alternative check:
Compile examples using dot vs arrow vs scope operator to see which combinations are allowed.



Why Other Options Are Wrong:
Class-to-member uses ::, not .Member-to-object order is reversed; the left must be the object.Namespaces use :: for qualified lookup, not .



Common Pitfalls:
Attempting to use dot on pointers (should use ->), or using dot with class names instead of instances for static members (should use ::).



Final Answer:
A class object and a member of that class.


More Questions from Objects and Classes

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion