Static HBITMAP

This is a discussion on Static HBITMAP within the C++ Programming forums, part of the General Programming Boards category; ok... so this is what I did: Code: class Shot { public: void move(); bool collide(); protected: POINT pos; static ...

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    Static HBITMAP

    ok... so this is what I did:
    Code:
    class Shot
    {
       public:
          void move();
          bool collide();
    
       protected:
          POINT pos;
          static HBITMAP image;
    };
    HBITMAP Shot::image;
    But it tells me I just defined image multiple times.

    So I looked at "Jamsa's C/C++/C# Programmer's bible", and it gave an example:

    Code:
    class Shot
    {
          static HBITMAP image;
       public:
          void move();
          bool collide();
    
       protected:
          POINT pos;
    };
    HBITMAP Shot::image;
    This worked, but then I tried to make a Bullet class that inherits from Shot:

    Code:
    class Bullet : public Shot
    {
    (...)
    };
    then in Bullet's constructor, when I tried accessing image, it said I couldn't access image because it was a private member of Shot. What am I doing wrong?

    p.s. dunno why, but just a sec ago I tried putting the declaration in the protected section again, and then in Bullet's header, "HBITMAP Bullet::image;" and it worked. Why is this???
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Code:
    class Shot
    {
          static HBITMAP image;
       public:
          void move();
          bool collide();
    
       protected:
          POINT pos;
    };
    HBITMAP Shot::image;
    By default, member variables are declared as private. Therefore, static HBITMAP image; is private in this instance. I think you can figure out the rest

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Thanks, but there's 3 main reasons why I made it private:

    a) I'm used to Java's static variables, which are a lot different.

    b) the book I was reading said "class Bullet: public Shot" makes all of Shot's variables public in Bullet. I didn't think so but who knows?... c++ is strange.

    c) When I declared image as protected, it said that I defined it twice; it didn't do that when I declared it as private.

    Does this (c) have something to do with inherited classes being able to access protected variables but not private ones?
    Last edited by Hunter2; 07-01-2002 at 12:55 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Does this (c) have something to do with inherited classes being able to access protected variables but not private ones?
    Yes.

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Yes.
    Elaborate please?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 02:22 PM
  3. uploading file to http server via multipart form data
    By Dynamo in forum C++ Programming
    Replies: 1
    Last Post: 09-03-2008, 05:36 AM
  4. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 06:05 PM
  5. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM

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