Declaration/Definition ??

This is a discussion on Declaration/Definition ?? within the C++ Programming forums, part of the General Programming Boards category; ok...this is a pretty lame question: I was reading Scott Meyer's "Effective C++", and on item E1, he writes this: ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    28

    Lightbulb Declaration/Definition ??

    ok...this is a pretty lame question:

    I was reading Scott Meyer's "Effective C++", and on item E1, he writes this:

    To limit the scope of a constant to a class, you must make it a member, and to ensure there's at most one copy of the constant, you must make it a static member:

    Code:
    class GamePlayer {
    private:
      static const int NUM_TURNS = 5;    // constant declaration
      int scores[NUM_TURNS];             // use of constant
      ...
    };
    There's a minor wrinkle, however, which is that what you see above is a declaration for NUM_TURNS, not a definition. You must still define static class members in an implementation file:

    Code:
    const int GamePlayer::NUM_TURNS;      // mandatory definition;
                                          // goes in class impl. file
    I doubt when he calls the "definition part" as "mandatory", because it should run fine without this explicit definition. (further...he is "defining" a private const member...is this allowed ??)

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    first of all i shall say that even though a lot of people get a chance to publish a book that doesn't mean that their way of viewing things is necessarily correct or straigh foward............

    second of all.... let me explain to you the idea of a "static" member.......

    You have probably thought of the data in each object as unique to that object and not shared among objects in a class.

    For instance you might want to have 4 "DoG" objects, which have their own private data members......perhaps like AGE and WEIGHT.....

    However, at times you'll want to keep track of a po ol of data. For example, you might want to know how many objects for a specific class have been created in your program.

    Static member variables are shared among all instances of a class. They are a compromise between global data
    , and member data.

    You can think of static member as belonging to the class rather than to the object. Normal member data is one per object, but static members are one per class.

    Going back to your example...... the explanation is kind of correct...... even though you see that 5 is being assinged to the static member.....it isn't so........ when you declare a static member the compiler doesn't allocate memory inside of the body of the class...you have to do it outside of the class.......in your last example you're doing so ......at that time you are defining this static member and set memory asside for it........ if you would have done so then the "linker" not a compiler would flag an error......

    Now, let's quickly go over the idea of public and private static members......

    ........
    - when you make your "static" member "public" then any function, even from outside of the scope of the class can access it, as well as you don't need an object of a class to access it.... hint: remember that static belong to the class not to an object...

    - on the other hand, if you make it "private" you would have to provide a some kind of an accessor funtion, and we know that in this case you would have to have an object to access the function first, which in result would access the "private" static data member....


    Suming it all up......if you want to make static data available to "anyone" is to make it public or to provide "A STATICE MEMBER FUNCTION" but i won't indulge into static member functions now.....

    however if you need more info on static member functions let us know...


    i hope this helped.....

    Regards,
    matheo917

  3. #3
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    Also on the subject of static members, because it's a member of the class and not single objects, you can access it without any objects created.

    Code:
    class Object {
    public:
          static int data;
          /*     */
    };
    
    int main()
    {
         Object::data = 0; // no errors
         return (0);
    }

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    28
    Originally posted by matheo917

    i hope this helped.....
    Thanks for taking time to write a long answer buddy.

    I already have an idea of static members, and public/private members. What I was contarying was how did the author define (assign a value to) a static private member of the class, outside the class?

    Secondly, he says that it is mandatory to define it outside the class...which I believe is not.

    And I believe the author is wrong here ( I copy-pasted the above code from the electronic edition CD of the books, so it is word-to-word same as the author wrote...)...

  5. #5
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    From the standard (my emphasis) -

    4 If a static data member is of const integral or const enumeration type, its declaration in the class
    definition can specify a constant-initializer which shall be an integral constant expression (5.19). In that
    case, the member can appear in integral constant expressions within its scope. The member shall still be
    defined in a namespace scope and the definition of the member in namespace scope shall not contain an
    initializer
    .
    Most compilers get this wrong. Infact Microsoft have only just got it working in a semi-correct manner.

Popular pages Recent additions subscribe to a feed

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