Operator Precedence in Member Access Operators
Member access operators . and -> have the highest precedence.
Below is the implementation of Operator Precedence in Member Access Operators:
#include <iostream>
using namespace std;
// Define a simple class
class MyClass {
public:
int myVar;
MyClass()
: myVar(10)
{
}
};
int main()
{
MyClass obj;
// Using dot operator to access member directly
cout << "obj.myVar: " << obj.myVar << endl;
// Create a pointer to the object
MyClass* ptr = &obj;
// Using arrow operator to access member via pointer
cout << "ptr->myVar: " << ptr->myVar << endl;
return 0;
}
Output
obj.myVar: 10 ptr->myVar: 10
Operator Precedence in Programming
Operator Precedence, also known as operator hierarchy, is a set of rules that controls the order in which operations are performed in an expression without parentheses. It is a fundamental concept in programming languages and is crucial for writing correct and efficient code.
Table of Content
- What is Operator Precedence?
- Operator Precedence in Arithmetic Operators
- Operator Precedence in Relational Operators
- Operator Precedence in Logical Operators
- Operator Precedence in Assignment Operators
- Operator Precedence in Bitwise Operators
- Operator Precedence in Conditional (Ternary) Operator
- Operator Precedence in Unary Operators
- Operator Precedence in Member Access Operators
- Operator Precedence in Type Cast Operators
- Importance of Operator Precedence