CPP class pointer doubt

Discussion in 'Programmer's Corner' started by ep.hobbyiest, Mar 30, 2016.

  1. ep.hobbyiest

    Thread Starter Member

    Aug 26, 2014
    91
    0
    Hi,
    I m new to cpp. I have doubt in following code. Doubt is regarding pointer. here is code
    Code (Text):
    1.  
    2. // pointer to classes example
    3. #include <iostream>
    4. using namespace std;
    5. class CRectangle {
    6.     int width, height;
    7.     public:
    8.     void set_values (int, int);
    9.     int area (void) {return (width * height);}
    10. };
    11.  
    12. void CRectangle::set_values (int a, int b) {
    13.     width = a;
    14.     height = b;
    15. }
    16.  
    17. int main () {
    18.     CRectangle a, *b, *c;
    19.     CRectangle * d = new CRectangle[2];
    20.     b= new CRectangle;
    21.     c= &a;
    22.     a.set_values (1,2);
    23.     b->set_values (3,4);
    24.     d->set_values (5,6);
    25.     (*(d+1)).set_values (7,8);
    26.     cout << "a area: " << a.area() << endl;
    27.     cout << "*b area: " << b->area() << endl;
    28.     cout << "*c area: " << c->area() << endl;
    29.     cout << "d[0] area: " << d[0].area() << endl;
    30.     cout << "d[1] area: " << d[1].area() << endl;
    31.     delete[] d;
    32.     delete b;
    33.     return 0;
    34. }
    35.  
    36.  
    from the line
    cout << "d[0] area: " << d[0].area() << endl;
    cout << "d[1] area: " << d[1].area() << endl;

    Here d is array which dynamically allocated. but when we are accessing area() function that time we have to use -> instead of ".". but, we are accessing set_value function using dot operator even though it is pointer.


    So, i m confused when to use arrow operator and when to use dot operator.
     
  2. vpoko

    Member

    Jan 5, 2012
    258
    47
    When you point to an array, the array's element 0 is at the address the pointer points to, element 1 is at that address plus the size of the pointer (also the size of each element of the array), element 2 is at that address plus twice the size of the pointer, and so on. When you say d->set_values(4,5) on line 24, you are hitting element zero of your array using the equivalent pointer notation (you use the arrow because d is, in fact, a pointer, and the arrow is syntactic sugar for deferencing. You could do the same thing with (*d).set_values(4,5) and not use the arrow). Likewise, you can work with d like an array (C mostly lets you treat a pointer to an array like the array itself, think of the [] as another shortcut for the dereference operator). d[0].set_values(4, 5). Why the dot in this case? Because d[0] is not a pointer, it is a reference to a CRectangle instance. References and pointers are similar but not identical, and one of the differences is that references dereference automatically.
     
    Last edited: Mar 31, 2016
  3. vpoko

    Member

    Jan 5, 2012
    258
    47
    No, the OP was trying to understand it, not saying something was wrong with it.
     
Loading...