Like Tree7Likes

Private static member set to NULL directly?

This is a discussion on Private static member set to NULL directly? within the C++ Programming forums, part of the General Programming Boards category; Hey people, found the following in the book I work with: Code: class Singleton { private: static Singleton *instanz; Singleton() ...

  1. #1
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197

    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.)
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Debian Testing/Jessie / Linux 3.16-2-amd64 / KDE SC 4.14.1
    Compiler: gcc 4.9.1-16 with KDevelop 4.7.0

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Of course you can .
    Just #include<cstdlib>

    Btw...Consider using nullptr instead of NULL when writing C++ code.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    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.)
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Debian Testing/Jessie / Linux 3.16-2-amd64 / KDE SC 4.14.1
    Compiler: gcc 4.9.1-16 with KDevelop 4.7.0

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    dennis.cpp and MK27 like this.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    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...
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Debian Testing/Jessie / Linux 3.16-2-amd64 / KDE SC 4.14.1
    Compiler: gcc 4.9.1-16 with KDevelop 4.7.0

  6. #6
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    >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.
    dennis.cpp likes this.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #7
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    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++.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Debian Testing/Jessie / Linux 3.16-2-amd64 / KDE SC 4.14.1
    Compiler: gcc 4.9.1-16 with KDevelop 4.7.0

  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 10:26 AM. Reason: class scope != global scope ;)
    dennis.cpp likes this.
    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
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    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.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Debian Testing/Jessie / Linux 3.16-2-amd64 / KDE SC 4.14.1
    Compiler: gcc 4.9.1-16 with KDevelop 4.7.0

  10. #10
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    >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 10:25 AM.
    dennis.cpp and MK27 like this.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  11. #11
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    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.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Debian Testing/Jessie / Linux 3.16-2-amd64 / KDE SC 4.14.1
    Compiler: gcc 4.9.1-16 with KDevelop 4.7.0

  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.
    dennis.cpp likes this.

  13. #13
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    That's where Java takes the same road again.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Debian Testing/Jessie / Linux 3.16-2-amd64 / KDE SC 4.14.1
    Compiler: gcc 4.9.1-16 with KDevelop 4.7.0

  14. #14
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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.

Page 1 of 2 12 LastLast
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, 04: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21