[Newbie Question] Static members

Discussion in 'C++' started by Manuel, Dec 26, 2005.

  1. Manuel

    Manuel Guest

    Hi!
    Anyone can explain me why this work:

    --------------------------
    class Rect
    {
    public:
    Rect()
    {num ++;}

    ~Rect()
    {num --;}

    static void pippo(){num ++;};

    protected:
    static int num;

    };
    int num = 0;

    main(){}
    -------------------------

    and this not work?

    -------------------------
    class Rect
    {
    public:
    Rect()
    {num ++;}

    ~Rect()
    {num --;}

    static void pippo();

    protected:
    static int num;

    };
    int num = 0;
    void Rect::pippo() {num ++;}
    main(){}

    ---------------------------

    Thanks!
     
    Manuel, Dec 26, 2005
    #1
    1. Advertising

  2. Manuel

    Marchello Guest

    > and this not work?
    > -------------------------
    > class Rect
    > {
    > public:
    > Rect()
    > {num ++;}
    >
    > ~Rect()
    > {num --;}
    >
    > static void pippo();
    >
    > protected:
    > static int num;
    >
    > };
    > int num = 0; // Maybe errors occurs here ?!
    > void Rect::pippo() {num ++;}
    > main(){}


    try to write:
    int Rect::num = 0;
     
    Marchello, Dec 26, 2005
    #2
    1. Advertising

  3. Manuel

    Jim Langston Guest

    "Manuel" <> wrote in message
    news:43afb6e7$0$1065$...
    > Hi!
    > Anyone can explain me why this work:
    >
    > --------------------------
    > class Rect
    > {
    > public:
    > Rect()
    > {num ++;}
    >
    > ~Rect()
    > {num --;}
    >
    > static void pippo(){num ++;};
    >
    > protected:
    > static int num;
    >
    > };
    > int num = 0;
    >
    > main(){}
    > -------------------------
    >
    > and this not work?
    >
    > -------------------------
    > class Rect
    > {
    > public:
    > Rect()
    > {num ++;}
    >
    > ~Rect()
    > {num --;}
    >
    > static void pippo();
    >
    > protected:
    > static int num;
    >
    > };
    > int num = 0;
    > void Rect::pippo() {num ++;}
    > main(){}
    >
    > ---------------------------
    >
    > Thanks!


    Actually, it's fairly simple. Rect::pippo() is static. Which means it's
    only supposed to be used on static variables. They have no this pointer.
    Neighter of those should work.

    change num to static and it should work.

    static int num;

    then outside the class

    int Rect::num = 0;

    I can not tell you why your first version compiled. It shouldn't of as far
    as I'm aware.
     
    Jim Langston, Dec 26, 2005
    #3
  4. Manuel

    Manuel Guest

    Marchello wrote:

    >
    > try to write:
    > int Rect::num = 0;
    >
    >



    It work!
    Thanks you very much!
     
    Manuel, Dec 26, 2005
    #4
  5. Manuel

    majianan Guest

    Re: Static members

    I have seen the first program in the <Effective C++> : Item 14:Make
    sure base classes have virtual destructors.
     
    majianan, Dec 26, 2005
    #5
  6. Manuel

    Manuel Guest

    Re: Static members

    majianan wrote:
    > I have seen the first program in the <Effective C++> : Item 14:Make
    > sure base classes have virtual destructors.
    >


    What's <Effective C++> ?
    A template for debug?
     
    Manuel, Dec 26, 2005
    #6
  7. Manuel

    Manuel Guest

    Jim Langston wrote:

    > change num to static and it should work.
    >
    > static int num;


    This was already done...or you mean I should write this in some other
    places?

    >
    > then outside the class
    >
    > int Rect::num = 0;



    Yes. This was the error!
    Thanks,

    Manuel
     
    Manuel, Dec 26, 2005
    #7
  8. Re: Static members

    On Mon, 26 Dec 2005 12:55:05 +0100, Manuel
    <> wrote:

    >majianan wrote:
    >> I have seen the first program in the <Effective C++> : Item 14:Make
    >> sure base classes have virtual destructors.
    >>

    >
    >What's <Effective C++> ?
    >A template for debug?


    One of several very useful books by Scott Meyers.

    --
    Bob Hairgrove
     
    Bob Hairgrove, Dec 26, 2005
    #8
  9. Manuel

    Manuel Guest

    Re: Static members

    Bob Hairgrove wrote:

    >
    > One of several very useful books by Scott Meyers.
    >



    Ooops...sorry for stupid question...
     
    Manuel, Dec 26, 2005
    #9
  10. Manuel

    Guest

    Re: Static members

    U must use Class scope, while acessing Static class members,...
    I think u need to brush-up c++ once...








    Manuel wrote:
    > Bob Hairgrove wrote:
    >
    > >
    > > One of several very useful books by Scott Meyers.
    > >

    >
    >
    > Ooops...sorry for stupid question...
     
    , Dec 26, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben
    Replies:
    3
    Views:
    420
  2. SaravanaKumar
    Replies:
    6
    Views:
    9,408
    Tony Morris
    Oct 19, 2004
  3. JFCM
    Replies:
    4
    Views:
    5,739
  4. Rakesh Sinha
    Replies:
    4
    Views:
    1,853
    Rakesh Sinha
    Jan 13, 2005
  5. Hicham Mouline
    Replies:
    5
    Views:
    2,377
    James Kanze
    Dec 19, 2008
Loading...

Share This Page