private data initialization

This is a discussion on private data initialization within the C++ Programming forums, part of the General Programming Boards category; I'm just a little confused on how this works Code: 15: private: 16: int itsAge; 17: static int HowManyCats; 18: ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    48

    private data initialization

    I'm just a little confused on how this works

    Code:
    15: private:
    16:   int itsAge;
    17:   static int HowManyCats;
    18:   };
    19:
    20: int Cat::HowManyCats = 0;
    The Cat Class has a static variable HowManyCats that is private. But it is initialized outside of the declaration or any other member function. How come it is accesible this way?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Every static data member must be defined, not just declared, since it exists outside of any instance of the class. It's not accessible.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    48
    Quote Originally Posted by tabstop View Post
    Every static data member must be defined, not just declared, since it exists outside of any instance of the class. It's not accessible.
    It's not accessible?

    I get that it exists regardless of any Cat objects, and that it has to be defined. But how can it be defined outside of the class, or without an object of that class, if it is private data.

    Are you saying that you are able to initialize it with a value from anywhere but cannot change it thereafter?

    I'm a little confused by this, I've been dealing with public accessor methods and this seems to completely break the rules to call a private variable directly.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by MattJ812 View Post
    Are you saying that you are able to initialize it with a value from anywhere but cannot change it thereafter?
    Pretty much. You are able to initialize it when it is defined (i.e., the line that starts "int"). I believe you are not required to do so. Since only one definition of the variable can exist, and any class author who knows enough to use one of these would make the definition, then that means that any "normal" user of the class is locked out from using the variable as well.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    48
    So for dummies this is just an initialization that is required right?

    Can this be done in the class declaration?

  6. #6
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    Quote Originally Posted by MattJ812 View Post
    So for dummies this is just an initialization that is required right?

    Can this be done in the class declaration?
    You can't initialize any member variable inside the class. The only one that you can initialize inside a class is "const static".
    "All that we see or seem
    Is but a dream within a dream." - Poe

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The standard says
    Quote Originally Posted by C++ standard clause 9.4.2, paragraph 2
    The declaration of a static data member in its class definition is not a definition and may be of an
    incomplete type other than cv-qualified void.
    So I'm pretty sure the answer is "no".

  8. #8
    Registered User
    Join Date
    Dec 2010
    Posts
    48
    ok cool this is beginning to make some sense thanks.

    EDIT...incomplete type? cv-qualified void?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by MattJ812 View Post
    ok cool this is beginning to make some sense thanks.

    EDIT...incomplete type? cv-qualified void?
    Incomplete type means "I don't know what this type is, exactly, yet." For instance:
    Code:
    class A; //this is an incomplete type: we know the name, but not what it is, yet
    
    class B {
        private:
            int some_data, other_data, more_data;
            static A *relevant;
    };
    We can declare the relevant variable here; we know A* is a type, we just don't know what it is. After we declare class A, we can then define relevant at that time. (And even if we had declared relevant to be of type A, not A*, we'd still be okay.)

    (You can have a function that returns void, for instance, or accepts void; but you can't declare a variable to be of type void, is all the other part says.)

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,433
    Quote Originally Posted by nimitzhunter
    You can't initialize any member variable inside the class. The only one that you can initialize inside a class is "const static".
    More accurately, you can only initialise const static integral member variables within a class definition.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 02:27 AM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. webBrowser problem
    By algi in forum C# Programming
    Replies: 7
    Last Post: 08-19-2005, 09:35 AM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. Not able to access private data members
    By smitsky in forum C++ Programming
    Replies: 31
    Last Post: 05-09-2004, 07:06 PM

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