Thread: Private static member set to NULL directly?

  1. #1
    spaghetticode
    Guest

    Question Private static member set to NULL directly?

    Hey people,

    found the following in the book I work with:

    Code:
    class Singleton {
    
        private:
    
            static Singleton *instanz;
            Singleton() { }
    
        public:
    
            static Singleton* getInstanz() {
    
                if (instanz == NULL) instanz = new Singleton();
                return instanz;
    
            }
    
    };
    
    Singleton* Singleton::instanz = NULL;
    
    int main() {
    
        Singleton* obj1 = Singleton::getInstanz();
        Singleton* obj2 = Singleton::getInstanz();
    
        cout << " obj1: " << &(*obj1) << endl;
        cout << " obj2: " << &(*obj2) << endl << endl;
    
        return 0;
    
    }
    Why can I set
    Code:
    Singleton::instanz
    directly to
    Code:
    NULL
    above
    Code:
    main()
    when it's private?

    (May be a stupid question, but I worked through a whole chapter of classes, constructors, members, and now I feel a bit overwhelmed with all the new things.)

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Of course you can .
    Just #include<cstdlib>

    Btw...Consider using nullptr instead of NULL when writing C++ code.

  3. #3
    spaghetticode
    Guest
    But what I have learned by now is that you always need a member function to access private members. And what's that about cstdlib? The book just includes iostream, and with gcc this code perfectly compiles (-Wall -pedantic). What am I missing here?

    (Thanks btw for that hint to nullptr - since my textbook uses NULL, I will research about that on this very website.)

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Well.. NULL is defined in <cstdlib>

    Sorry.. I misread your question(halfway)...and inferred that it wasn't compiling, when it was just the opposite!

    In that case, whether you can define it outside, using the scoped notation does not depend on whether it is private or not.
    The visibility comes into play when you try it to access it from a function which is not a friend.
    (Following your trend of thought...it would have been impossible to define private methods in an implementation file if this wasn't allowed)

    I trust that you understand why the definition for a static data member is needed..so skipping that.

  5. #5
    spaghetticode
    Guest
    Ahhhhhhhh! *head -> table*

    I seem to have misinterpreted the whole line. That's what I meant when I said I felt confused after all the new stuff. I see, that's just the definition. Now I feel clearer about that, thanks a lot. I guess I will have to re-read the whole chapter at least one more time to have it all sink in...

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    >I guess I will have to re-read the whole chapter at least one more time to have it all sink in...
    Or you could write a nice program.

  7. #7
    spaghetticode
    Guest
    I don't think I already know enough to find any problem where I would need a code like the above. Keep in mind that I just *started* working on OOP, classes and stuff in C++.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by dennis.cpp View Post
    I don't think I already know enough to find any problem where I would need a code like the above.
    Singleton's have a pretty limited use value IMO, but a more general lesson you can take from that is how to use non-const static class members. Try this:

    Code:
    #include <iostream>
    
    class Thing {
        public:
            static int n;
            Thing(int a) { n = a; };
    };
     
    int Thing::n = 5;
    
    using namespace std;
     
    int main() {
    	Thing a(2), b(6);
    	cout << a.n << endl;
    	return 0;
    }
    Notice the output is 6. Static members allow one object to set a value for all other instances of a class, but they must be initialized in class::scope outside of a (non-friend) function (which, as manasij7479 points out, is why you are allowed to do this even with private members).
    Last edited by MK27; 12-01-2011 at 11:26 AM. Reason: class scope != global scope ;)
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    spaghetticode
    Guest
    Thanks you too. I remember the use of class members from Java, actually. But having to define them in the global scope is something I'll need to get used to.

  10. #10
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    >But having to define them in the global scope is something I'll need to get used to.
    You are NOT defining them in global scope.
    It is still in the class scope(the :: operator is there for that reason).

    For methods, there is no difference between :
    Code:
    class foo
    {
         int bar()
        { 
             return 0;
        }
    };
    and
    Code:
    class foo
    {
        int bar();
    };
    int foo::bar()
    { 
        return 0;
    }
    For static data members ... you must define them outside for the common memory to be set aside from the beginning.
    Last edited by manasij7479; 12-01-2011 at 11:25 AM.

  11. #11
    spaghetticode
    Guest
    You're right! Thanks again. Wah, Java was so much easier there. I will revise the whole chapter tomorrow and maybe, in addition, work through this page's tutorial section for classes.

  12. #12
    Registered User
    Join Date
    Nov 2011
    Location
    boston
    Posts
    5
    I dont know how much it explains static members and static functions but a static function can only modify static members or only call other static functions.

    however non static functions can call static functions and static members can be returned from a non static function.

  13. #13
    spaghetticode
    Guest
    That's where Java takes the same road again.

  14. #14
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Acorn View Post
    I dont know how much it explains static members and static functions but a static function can only modify static members or only call other static functions.
    ..Unless you give the static function an object to refer to.

  15. #15
    Registered User
    Join Date
    Nov 2011
    Location
    boston
    Posts
    5
    thanks mana i didnt know that. i just tested it out and your right.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Member is private..??
    By Programmer_P in forum C++ Programming
    Replies: 35
    Last Post: 06-05-2010, 12:59 AM
  2. Replies: 3
    Last Post: 04-10-2009, 02:20 AM
  3. private member wont add.
    By System_159 in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2006, 05:25 PM
  4. no change for private member ???
    By black in forum C++ Programming
    Replies: 7
    Last Post: 05-28-2004, 05:25 AM
  5. private data member with public set member function
    By Mario in forum C++ Programming
    Replies: 2
    Last Post: 05-28-2002, 10:53 AM