Thread: Can we have a pointer to a static class member ?

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    59

    Question Can we have a pointer to a static class member ?

    Hello Friends,

    Can there be a pointer to a class member which is static ?
    Please suggest..

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes. Have you tried?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    59
    Yes.. I tried, this is my code :
    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;
    }
    and this is the compilation error that i get : static_mem_ptr.cpp: In function ‘int main()’:
    static_mem_ptr.cpp:error: cannot convert ‘short int*’ to ‘short int test::*’ in initialization

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    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?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Elkvis View Post
    why do you have test:: in front of it?
    Probably because the syntax for a pointer to a member variable is:

    T A::* b = &Class::Member;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Elysia View Post
    Probably because the syntax for a pointer to a member variable is:

    T A::* b = &Class::Member;
    But that's entirely unnecessary for static members, and it makes the code more difficult to read.
    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?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It doesn't work at all with static members, but the OP did not know this. The OP did not know the correct syntax.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Aug 2013
    Posts
    59
    @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..

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by techie_san778
    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.*ptr
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Aug 2013
    Posts
    59
    @ 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.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by techie_san778
    now, i want to know why is a pointer to a static member declared as *ptr instead of <class name> :: *ptr ?
    Refer to a previous thread of yours:
    Quote Originally Posted by grumpy
    A pointer to member would be more completely named as "pointer to non-static member".
    Quote Originally Posted by Elysia
    A static member belongs to all or no instance; therefore, it makes no sense to bind it to a particular instance, so you can't create a pointer to a member int for a static int.
    In other words, you have to use a normal int*.
    In 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static member initialization with class template
    By threahdead in forum C++ Programming
    Replies: 6
    Last Post: 04-28-2011, 12:16 AM
  2. static pointer data member
    By suppu143 in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2011, 07:46 AM
  3. Can you define a static member function in a class?
    By meili100 in forum C++ Programming
    Replies: 2
    Last Post: 06-22-2008, 09:23 PM
  4. Static member of a class
    By Gravedigga in forum C++ Programming
    Replies: 2
    Last Post: 08-15-2005, 03:54 AM
  5. Why can I modify my static class member?
    By silverfoxtp in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2003, 04:24 AM