0
#include <iostream>
using namespace std;

class Sample
{
    public:
        int *pxx;
        int x;

        void setD(int y)
        {
            x=y;
        }

        void print()
        {
            int Sample::*px = &Sample :: x; 
            cout<<"\nx : "<<x;
            cout<<"\nAddress of x : "<<&x;
            cout<<"\nValue of X indirected through px :"<<this->*px;
            //cout<<"\nValue of X indirected through px :"<<*px; ERROR
            //cout<<"\nAddress of x i.e. px : "<<px; NO ERROR BUT UNDESIRED OUTPUT(The output is most of the time '1')
            //cout<<"\nAddress of x i.e. px : "<<this->px; ERROR
        }
};

I've read that when a pointer is declared using the syntax data type <class_name> :: * <pointer> = &<class_name> :: <variable_name> it acts like a class member, then why am I not allowed to execute those statements given in the comments of the above program(except for 1). Is there any difference between px and pxx(except that of scope)?

Zaid Khan
  • 786
  • 2
  • 11
  • 24
  • 2
    you can refer to http://stackoverflow.com/questions/670734/c-pointer-to-class-data-member regarding how to use pointer to class member. – user258367 Aug 15 '15 at 06:08
  • I have referred to it before. I just want to know what is the scope of this _Pointer to a member_ and whether it classifies as a **class member** or not? – Zaid Khan Aug 15 '15 at 07:17

1 Answers1

3

Well, no it is not supposed to "act like a class member". That's just not true. I don't know where you got it.

It is supposed to act as a value of pointer-to-member type. And such pointers require ->* or .* operators to dereference

cout << "\nValue of X indirected through px :" << this->*px;
cout << "\nValue of X indirected through px :" << (*this).*px;

And, as you can see, these operators require a specific object on the left-hand side. You cannot just dereference px by itself. It has no meaning by itself.

A value of pointer-to-data-member type is a language-level implementation of the low-level concept of "offset" (offset of data measured from the beginning of the containing object). In order find the actual data through given offset, you have to have a starting point from which to measure that offset. That is what the object expression of the left-hand side of -> or .* represents.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • I am not able to grasp "Pointer of Pointer-to-class-member"(Did you meant Pointer to Pointer/Double Pointer?). Can you elaborate on this please(with a simple example.) And why is the statement `cout<<"\nAddress of x i.e. px : "< – Zaid Khan Aug 15 '15 at 06:55
  • @Khan Zaid: "Pointer-to-member" is a special kind of entity, significantly different from a normal regular pointer. So, there are pointers of *regular kind* and there are pointers of *pointer-to-member kind*. On the second thought, maybe this redundant-sounding terminology should be avoided. One can say the same thing without it. (I'll try to rephrase the answer). And no, it has nothing to do with double pointers. – AnT stands with Russia Aug 15 '15 at 08:46
  • And why do you expect `cout << px;` to generate an error? This code does not attempt to *dereference* the pointer, meaning that what I said above about *dereferencing* such pointers does not apply. This code simply uses the fact that such pointers (just like regular pointers) can be converted to `bool` type. I.e. it is equivalent to `cout << (bool) px;` – AnT stands with Russia Aug 15 '15 at 08:49
  • Well don't these 'Pointers to Members' "have no meaning by itself", and then why does `cout<px;` generates an error then? Am I missing something? – Zaid Khan Aug 15 '15 at 09:07
  • @Khan Zaid: Well... There are valid expressions with pointers to members. And there are invalid expressions with pointers to members. `px`, `this->*px` and `(*this).*px` are valid expressions. `this->px` is an invalid one. Why does it surprise you? Why do you expect it compile? What should it do, in your opintion? – AnT stands with Russia Aug 15 '15 at 09:12
  • Well... If a pointer to a member really requires an object on its left side then `this->px` should be valid and `px` not? – Zaid Khan Aug 15 '15 at 09:17
  • @Khan Zaid: I never said that pointer-to-member requires an object on the LHS. I said: the operation of *dereferencing* a pointer-to-member requires an object on the LHS. Expression `px` does not *dereference* the pointer, so the LHS remark dos not apply. And expression `this->px` is simply invalid. – AnT stands with Russia Aug 15 '15 at 10:01
  • Sorry if my question sounds stupid, but why do we use this method, for example, why can't we use Method 1, code link [here.](http://justpaste.it/n30t) – Zaid Khan Aug 17 '15 at 14:11