Hello Friends,
Can there be a pointer to a class member which is static ?
Please suggest..
Hello Friends,
Can there be a pointer to a class member which is static ?
Please suggest..
Yes. Have you tried?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Yes.. I tried, this is my code :
and this is the compilation error that i get : static_mem_ptr.cpp: In function ‘int main()’:Code:#include <iostream> using namespace std; class test { public: static short num; }; short test :: num = 10; int main() { test t1; short test :: *ptr = &test :: num; cout << "\n num = "<< t1.*ptr; cout << "\n\n"; return 0; }
static_mem_ptr.cpp:error: cannot convert ‘short int*’ to ‘short int test::*’ in initialization
The error message is telling you that &test::num is a short int*, i.e., you should have written:
Code:#include <iostream> using namespace std; class test { public: static short num; }; short test :: num = 10; int main() { test t1; short* ptr = &test::num; cout << "\n num = "<< *ptr; cout << "\n\n"; return 0; }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
in main(), is ptr a member of test? why do you have test:: in front of it?
What can this strange device be?
When I touch it, it gives forth a sound
It's got wires that vibrate and give music
What can this thing be that I found?
@laserlight
When i change the line short test :: *ptr = &test :: num; to
short *ptr = &test :: num;
I get the compilation error message : ‘ptr’ cannot be used as a member pointer, since it is of type ‘short int*’.
Please suggest..
That is because you tried to use it like this: t1.*ptrOriginally Posted by techie_san778
Notice that I changed that to: *ptr
The error message should have included a line number that directed you to that line.
Did you try compiling my example program in its entirety? You should have done that before asking, and when you found that it compiled whereas your attempt didn't, you should have compared to see what was the significant differences, and only if you didn't understand why the difference was important, then should you ask about it. Otherwise, you're basically asking for syntax that you should be learning from a book, not asking in a forum.
Last edited by laserlight; 01-15-2015 at 12:58 AM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
@ laserlight,
had i found the syntax to declare a pointer to static member of a class in any text, i would not have asked it at the first place. I did not copy ur prgm, rather made the changes to my program and somehow i missed the 2nd line which was difference. But, i made changes to the line pointed by the compiler. So, mind you, i was sincere in my effort..
now, i want to know why is a pointer to a static member declared as *ptr instead of <class name> :: *ptr ?
Last edited by techie_san778; 01-15-2015 at 01:17 AM.
Refer to a previous thread of yours:Originally Posted by techie_san778
Originally Posted by grumpyIn other words, a pointer to a static member is treated similiarly as a pointer to say, a local variable in a function. Look back at the error message that you mentioned in post #3: it says that you have a short int*. Notice that it is just a pointer to a short int. It has nothing to do with the test class, other than the fact that you obtain it by referencing test::num, since num is in the scope of the test class, being a member of the test class.Originally Posted by Elysia
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)