Page Content

Tutorials

What Are The Accessing Members In C++ With Code Example

Accessing Members in C++

To access class, struct, and union members in C++, you require the -> (arrow) and. (dot) operators. These operators work with the member functions (methods) and data members of an object.

The Dot Operator ()

You can directly access object members with the dot operator (.).

  • Purpose: It retrieves a member (data or function) from a class-type object.
  • Syntax: object.member. An instance of a class, struct, or union must be the object in this case, and member is the name of a member function or data member of that type.
  • Access Control: Only public object members can be directly accessed with the dot operator. No one outside the class can access secret or protected members using this operator. Object-oriented programming requires data hiding.
  • Accessing public member functions directly on the current object is illustrated in the example via current.init() and current.display().

Code Example using the Dot Operator:

#include <iostream> // Needed for cout/cin.
#include <string>   // Needed for string.
class person     {
    std::string name; // private member (by default)
    int age;          // private member (by default)
public:
    void getdata(void);   // public method 
    void display(void);   // public method
};
void  person :: getdata ( void )
{
    std::cout << "enter name: "; // Modified for clarity
    std::cin >> name;
    std::cout << "enter age: ";  // Modified for clarity
    std::cin >> age;
}
void person :: display() // Original source has display() without class scope, fixed for valid C++.
{
    std::cout << "\n name: " << name;
    std::cout << "\n age: " << age << std::endl; // Added std::endl for output.
}
int main( )
{
    person p;        // Defines an object 'p' of type person 
    p.getdata();     // Calls the public method getdata() for the object 'p' 
    p.display();     // Calls the public method display() for the object 'p' 
    // p.age = 30;   // This would typically be an error if 'age' is private (which it is by default here)
    return 0;
}

Output

enter name: Govindhtech
enter age: 2
 name: Govindhtech
 age: 2

The Arrow Operator ()

Access object pointer members with the arrow operator (->). A convenient shorthand for using the dot operator after dereferencing a pointer.

  • Purpose: Combines the dot (.) operator with dereference () operator. PTR ->ptr is a synonym for mem.mem. With pointers, this is especially helpful for eliminating clumsy brackets.
  • Syntax: Member from objectPointer. An object of a class, struct, or union type must be referenced by objectPointer in this case.
  • Overloading: It is possible to overload the -> operator for user-defined types. For smart pointers and iterator classes, overloading operator->() is frequently used to provide them “pointer-like” behaviour and maybe add additional logic. A non-static member function of the class must be an overloaded operator->(), and the type of its return must be either an object of a class type that defines an overloaded operator->() or a pointer to a class type.
  • Using the -> operator while working with pointers, this illustrates how to invoke a member function of a class and access a data member of a struct.

Code Example using the Arrow Operator:

#include <iostream> // Not in source, but needed for cout.
class myclass {
public:
    int i; // Public data member for direct access
    // Overloaded arrow operator, demonstrating its behavior 
    myclass *operator->() {
        return this; // Returns a pointer to the current object
    }
};
int main() {
    myclass ob; // Create an object 'ob'
    
    // Access 'i' directly using the dot operator
    ob.i = 100;
    std::cout << "Accessed via dot operator: " << ob.i << std::endl;
    // Access 'i' using the overloaded arrow operator 
    // Here, ob->i is equivalent to (ob.operator->())->i, which resolves to this->i or ob.i
    ob->i = 10; // This is the same as ob.i = 10; due to overloading
    std::cout << "Accessed via overloaded arrow operator: " << ob->i << std::endl; // 
    // Example with a regular pointer (not using overloaded operator->)
    myclass* pOb = &ob; // Get a pointer to the object
    pOb->i = 20; // Access 'i' through the pointer using the arrow operator
    std::cout << "Accessed via regular pointer and arrow operator: " << pOb->i << std::endl;
    // Further examples from sources:
    // A pointer 'pb' to an Ival_box object 
    // pb->prompt();
    // int val = pb->get_value();
    // An array of pointers 'accPtr' to objects 
    // if( accPtr[i] != NULL)
    //    accPtr[i]->display(); // To output 
    return 0;
}

Output

Accessed via dot operator: 100
Accessed via overloaded arrow operator: 10
Accessed via regular pointer and arrow operator: 20

Key Differences and Relationships

  • Operand Type: The dot operator (.) works on objects, while the arrow operator (->) works on pointers.
  • Precedence: So, (ptr) takes precedence over the dereference operator.member needs brackets, so ptr->member is clearer. The. and ->* operators have lower precedence than the function call operator when executing a member function through a pointer-to-member, therefore braces like (pScreen->*pmf)() are needed.
  • Static Members: Since static members aren’t linked to any particular object, they can’t be accessed directly using either. or ->. ClassName::staticMember is an example of this, where the class name is used with the scope resolution operator (::).

Finally, whether you have the object directly or a pointer to it determines whether you should use. or ->.

Agarapu Geetha
Agarapu Geetha
My name is Agarapu Geetha, a B.Com graduate with a strong passion for technology and innovation. I work as a content writer at Govindhtech, where I dedicate myself to exploring and publishing the latest updates in the world of tech.
Index