-1

I have found these examples in the book of c++ written by Herbert Schildth

The first example

#include<iostream>
#include<cstring>
#include<new>
using namespace std;
class Balance
{
char name[80];
double cur_bal;
public:
Balance(){}
void set(double n,char *s)
{
    cur_bal=n;
    strcpy(name,s); 
}
void get(double &n,char *s)
{
    n=cur_bal;
    strcpy(s,name);
}
};
int main()
{
Balance *p;
char name[80]; double n;
try{ p= new Balance[2]; }
catch(bad_alloc e){ cout<<"alloctaion Failed\n"; return 1;}
p[0].set(121.50,"harsh");   //using DOT to access member function
p[1].set(221.50,"sparsh");  //using DOT to access member function
p[0].get(n,name);
return 0;
}

The second example

#include<iostream>
#include<cstring>
#include<new>
using namespace std;
class balance
{
 double cur_bal;
 char name[80];
 public:
  void set(double n,char *s)
  {
  cur_bal=n;
  strcpy(name,s);
 }
 void get_bal(double &n,char *s)
 {
   n=cur_bal;
   strcpy(s,name);
 }
};
int main()
{
balance *p;
char s[80];
double n;
try{p=new balance;}
catch(bad_alloc e){cout<<"Allocation Failed";  return 1; }
p->set(124.24,"harsh"); // using ARROW to access member function
 p->get_bal(n,s);   // using ARROW to access member function
 return 0;
}

Please, I don't need the difference between the two operators(it is already on SO link to that question), but simply explain why they are using arrow and dot respectively, and how do I understand when to use what?

Rohit Sah
  • 27
  • 6
  • **pointer** to object = **arrow**, **O**bject (O) = dot. – P.W Nov 21 '18 at 04:34
  • Subscripting a pointer by n dereferences the object **pointer past n points to**. For example `const char* ptr = "Hello"`, `ptr[1]` gets `'e'` and is equivalent to `*(ptr+1)` – William Lee Nov 21 '18 at 04:43

2 Answers2

2

To keep it simple, whenever you are accessing the members (data members, member functions) via pointer then you use arrow. If you are the members (data members, member functions) through the variable or object then use dot. For example

int main()
{
    balance b; // Or balance b = new b; (and delete it at the end of program)
    char s[80];
    double n;
    b.set(124.24,"harsh"); // using DOT to access member function
    b.get_bal(n,s);   // using DOT to access member function
    return 0;
}

Here b is an object of class balance not the pointer reference to it. In your first example p is acting as the array of objects which will contain the objects of type Balance. So every index of p will contain the Balance class objects and the members would be accessed by DOT operator iterating over each entry in array.

In second example p itself is a pointer to the class balance. So Arrow would be required to access the members

Brij Raj Kishore
  • 1,595
  • 1
  • 11
  • 24
2

The .-operator is used to access members of an object.

The ->-operator is used to access members of a pointer.


Actually,

a->b

is merely syntactic sugar for

(*a).b

which first dereferences a pointer (i.e. "making" it an object) and then accesses its member b using the .-operator.


As for you wanting to know

understand when to use what

here's a simple guideline:

If you have a variable a which is

  • a pointer, use ->
  • an object, use .

See this for more information on what even is an "object" and what is a "pointer".

Thomas Flinkow
  • 4,845
  • 5
  • 29
  • 65