Jump to content
  • Advertisement
Sign in to follow this  
DrZoidberg

Inheritance Question ( ... about constructors)

This topic is 4890 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hello, i have two questions actually (not really related ... and one sorta dumb) ... ok first off the main question on inheritance and constructors. consider this class ...
class CSprite {
  private:
    BITMAP *image;
  protected:
    int x, y;
    int width, height;
    int speedx, speedy;
    // and so on ...
  public:
    CSprite(BITMAP *image_, int x_, int y_, int width_, int height_)
      : image(image_), x(x_), y(y_), width(width_), height(height_) { }
    virtual void draw();
    virtual void move();
    // ...
};

// ok now a derived class
// and you might ask why i would have the image var as private, and well becasue
// some classes that will derive from this one might want to be animated ...
// and will need to have a vector of BITMAP's ...

class CSomeDude : public CSprite {
  private:
    std::vector < BITMAP * > images;
  public:
    CSomeDude(BITMAP *image_, int x_, int y_, int width_, int height_)
      : CSprite( WHEREDOESTHISGO, x_, y_, width_, height_) {
    // generate some rotated images or something to fill the vector
    }
    // ...
};

ok now you see CSprite needs a bitmap to in its constructor but the bitmap is private so it does not exsist in derived classes ... but when i derive a class and pass some vars to the base class constructor where the hell does the bitmap go in the first parameter? (if someone accidently doesnt put NULL as the first param when creating an instance of the derived object) ... confusing ... now for the dumb and unrelated question ... say i have 5 files, and for simplicities sake only 1 header (with the 4 other as source files) ... now i want to declare some global bitmap variables. BITMAP *g_something ... the other source files may use this definition ... when i link this i get errors saying multiple definitions, in any file that uses it, even though i dont redefine it ... if i throw an extern in the header file (not the source files) and then define it later in the main function of the program it will work ... however this problem doesnt occur if i declare a variable as const. im using dev-cpp and i hear that multiple source file projects have bugs sometiems ... could this by a bug, or am i just dumb? thanks for any help in advance.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
ok now you see CSprite needs a bitmap to in its constructor but the bitmap is private so it does not exsist in derived classes

It exists, but it isn't accessible. When a derived object is created, this is one possible layout in memory:
-----------------
| Base object
-----------------
|
| Derived
| Object
-----------------
Quote:
where the hell does the bitmap go in the first parameter?

If you have a class called EnemySprite, you'd do this:
class EnemySprite : public CSprite
{
public:
EnemySprite(BITMAP* image, int x, int y, int width, int height) :
CSprite(image, x, y, width, height)
{
}

Since objects are constructed inside out, you need to call a specific base class constructor before you initialize the EnemySprite member variables. To do this, you need to pass down all of the parameters it is expecting.

Quote:
however this problem doesnt occur if i declare a variable as const.

Constants have implicit static linkage, which just means they are confined to a single translation unit (e.g. cpp file).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!