S
somenath
I am trying to understanding the difference between the pointer to the no data member and pointer to the first member of class.
Also I have learned that “The value returned from taking the member’s address, however, is always bumped up by 1”.
To understand this concept I wrote the following simple program.
#include<iostream>
using namespace std;
class A
{
public:
int data;
int data2;
};
int main(void)
{
A a1;
int A::*first_data_member =&A::data;
int A::*second_data_member =&A::data2;
int A::*no_data_member ;
cout<<"no_data_member = "<<(no_data_member)<<endl;
cout<<"first_data_member = "<<(first_data_member)<<endl;
cout<<"second_data_member = "<<(second_data_member)<<endl;
return 0;
}
The output of the above program is as follows.
=================================================
no_data_member = 1
first_data_member = 1
second_data_member = 1
================================================
I am not able to understand the output of the program.
My expectation is if no_data_member is 1 then first_data_member sould be = no_data_member + 1 and second_data_member should be = first_data_member +1.
Please let me know where I am going wrong?
Also I have learned that “The value returned from taking the member’s address, however, is always bumped up by 1”.
To understand this concept I wrote the following simple program.
#include<iostream>
using namespace std;
class A
{
public:
int data;
int data2;
};
int main(void)
{
A a1;
int A::*first_data_member =&A::data;
int A::*second_data_member =&A::data2;
int A::*no_data_member ;
cout<<"no_data_member = "<<(no_data_member)<<endl;
cout<<"first_data_member = "<<(first_data_member)<<endl;
cout<<"second_data_member = "<<(second_data_member)<<endl;
return 0;
}
The output of the above program is as follows.
=================================================
no_data_member = 1
first_data_member = 1
second_data_member = 1
================================================
I am not able to understand the output of the program.
My expectation is if no_data_member is 1 then first_data_member sould be = no_data_member + 1 and second_data_member should be = first_data_member +1.
Please let me know where I am going wrong?