| | |
Pointer to a data member
![]() |
Hi,
I am bit confused with the concept of pointer to a data member of a class.
In the below program what actually will the pointer variable be storing? When i tried printing the value of address I got value 1, Whatt does it mean?
class A{
public:
int d;
};
void main(void)
{
int A:
p = &(A::d);
}
I tried creating an object of class A and assigning the address of the data member to p then also my VC compiler is throwing error 'cannot convert from 'int *' to 'int A:
''. Why this error is thrown when b is of type A. Same with the gcc compiler also.
class A{
public:
int d;
};
void main(void)
{
A bb;
bb.d = 10;
int A:
p = &(A::d);
p = &(bb.d);
}
I am bit confused with the concept of pointer to a data member of a class.
In the below program what actually will the pointer variable be storing? When i tried printing the value of address I got value 1, Whatt does it mean?
class A{
public:
int d;
};
void main(void)
{
int A:
p = &(A::d);}
I tried creating an object of class A and assigning the address of the data member to p then also my VC compiler is throwing error 'cannot convert from 'int *' to 'int A:
''. Why this error is thrown when b is of type A. Same with the gcc compiler also.class A{
public:
int d;
};
void main(void)
{
A bb;
bb.d = 10;
int A:
p = &(A::d);p = &(bb.d);
}
Where are you getting this from?
In C you declare Type <variable name> ;
so to declare a pointer to an integer using the * dereference symbol you do this:
try this:
When we print p we get the value of p itself (which is a memory address where the value of x is being stored) When we print * p we dereference to that address and get the actual bits from that memory location 10 (remember you declare the pointer to be int so the compiler knows to get 4 bytes of memory starting at the address stored in p and it knows the bits stored in those four bytes represent an integer, so cout can diplay them correctly as the decimal integer 10.
C++ Syntax (Toggle Plain Text)
int A::*p = &(A::d);
so to declare a pointer to an integer using the * dereference symbol you do this:
C++ Syntax (Toggle Plain Text)
int * p = &someintegervariable
try this:
C++ Syntax (Toggle Plain Text)
#include <iostream> class A { public: int x; }; int main(int argc, char *argv[]) { A a; a.x = 10; int * p = &a.x; std::cout << "Memory = " << p << "\n"; std::cout << "Value pointed to = " << *p << "\n"; std::cin.get(); return 0; }
When we print p we get the value of p itself (which is a memory address where the value of x is being stored) When we print * p we dereference to that address and get the actual bits from that memory location 10 (remember you declare the pointer to be int so the compiler knows to get 4 bytes of memory starting at the address stored in p and it knows the bits stored in those four bytes represent an integer, so cout can diplay them correctly as the decimal integer 10.
Last edited by hollystyles; Jun 30th, 2006 at 8:52 am.
•
•
•
•
Originally Posted by hollystyles
Where are you getting this from?
In C you declare Type <variable name> ;C++ Syntax (Toggle Plain Text)
int A::*p = &(A::d);
so to declare a pointer to an integer using the * dereference symbol you do this:
C++ Syntax (Toggle Plain Text)
int * p = &someintegervariable
try this:
C++ Syntax (Toggle Plain Text)
#include <iostream> class A { public: int x; }; int main(int argc, char *argv[]) { A a; a.x = 10; int * p = &a.x; std::cout << "Memory = " << p << "\n"; std::cout << "Value pointed to = " << *p << "\n"; std::cin.get(); return 0; }
When we print p we get the value of p itself (which is a memory address where the value of x is being stored) When we print * p we dereference to that address and get the actual bits from that memory location 10 (remember you declare the pointer to be int so the compiler knows to get 4 bytes of memory starting at the address stored in p and it knows the bits stored in those four bytes represent an integer, so cout can diplay them correctly as the decimal integer 10.
But what my doubt is if u do something like what I have shown, the compiler will not throw any error. If that is the case, what is the thing which the pointer is storing?
I have seen in some material also that u can access the data member thru this way. (not sure whether this is a right way)
•
•
•
•
'cannot convert from 'int *' to 'int A:''. Why this error is thrown when b is of type A
Also A is a class, where as b is an instance of class A. Which are you trying to point to ?
Last edited by hollystyles; Jun 30th, 2006 at 9:20 am.
•
•
•
•
I have seen in some material also that u can access the data member thru this way
A pointer to a member requires three distinct parts. First, you need to create a pointer (qualified by the class whose member it points to):
Then you need to assign the member you wish to point to by taking its address (also qualified by the class name):
Finally, to actually point to an object, you need an existing object and you need to use a member access operator on it using the dereferenced pointer as the "member":
This covers pointers in pretty good detail, and there's a bit of coverage on pointers to members.
C++ Syntax (Toggle Plain Text)
#include <iostream> class test { public: int i; }; int main() { int test::*p; }
C++ Syntax (Toggle Plain Text)
#include <iostream> class test { public: int i; }; int main() { int test::*p; p = &test::i; }
C++ Syntax (Toggle Plain Text)
#include <iostream> class test { public: int i; }; int main() { int test::*p; p = &test::i; test a; a.*p = 12345; }
Last edited by Narue; Jun 30th, 2006 at 9:53 am.
I'm here to prove you wrong.
Ok I get it now.
C++ Syntax (Toggle Plain Text)
#include <iostream> class A { public: int x; }; int main(int argc, char *argv[]) { A a, b; a.x = 10; b.x = 20; int * p = &a.x; int A::*p2 = &A::x; std::cout << "p points to a.x ONLY @ " << p << "\n"; std::cout << "a.x = " << *p << "\n"; std::cout << "p2 can be used for a.x " << a.*p2 << "\n"; std::cout << "Or for b.x !! wow. " << b.*p2 << "\n"; std::cin.get(); return 0; }
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: c++ or shell script to delete some files
- Next Thread: ostringstream overloading
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ char class classes code coding compaitibility compile console conversion count delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error factorial file floatingpoint forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple net news node number numbertoword output parameter payment pointer problem program programming project projectassignmenthelp protection python random rank read recursion reference rpg skills string strings temperature template test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






