Jump to content

Featured Replies

Posted

I'm bashing my head into a wall over this one.

 

I've got one main class, BitmapObject, which I'm trying to get derived classes from. I create a publically inherited class, PaddleObject, everything's just peachy.

 

When I try to display it on a window using the GDI (BitBlt of an DC, Selected with a BITMAP), with a public funciton inside BitmapObject, it refuses to show itself.

 

HOWEVER, when I make a BitmapObject, and use the very same function, the image appears in all it's glory.

 

WHY THE FUCK IS THIS HAPPENING!?

Can you post the definition of the BitmapObject and PaddleObject classes?

 

Would make it a bit easier to understand what you're aiming for and where you're having an issue.

erm not really sure whats going on but if you have an object and say create an instance of it newBoject = new object(); whatever then to show it u need

newObject.show(); i know thats some thing u have to do in java. check the class your inheriting from to see what objects it allready has. this may be no help to you add all because i dont totaly know whats the problem

Lets say we have a member function Draw() in BitmapObject.

Now, to inherit, we do this:

class PaddleObject : public BitmapObject
{
 // ...
 public:
   void Draw() const; // Exactly same declaration as in BitmapObject.
   // ...
};

 

Then, to make the Draw function in PaddleObject, we can do like this:

void PaddleObject::Draw() const
{
 BitmapObject::Draw(); // Do whatever BitmapObject does
 
 // Here, do whatever extra you want PaddleObject to do (if you have nothing here, you might want to think about a different method than inheriting)
}

  • Author
Lets say we have a member function Draw() in BitmapObject.

Now, to inherit, we do this:

class PaddleObject : public BitmapObject
{
 // ...
 public:
   void Draw() const; // Exactly same declaration as in BitmapObject.
   // ...
};

 

Then, to make the Draw function in PaddleObject, we can do like this:

void PaddleObject::Draw() const
{
 BitmapObject::Draw(); // Do whatever BitmapObject does
 
 // Here, do whatever extra you want PaddleObject to do (if you have nothing here, you might want to think about a different method than inheriting)
}

 

So I have to retype the prototype plus the function when I inherit? What? I thought inhertiting meant that everything was inherited, including functions.

So I have to retype the prototype plus the function when I inherit? What? I thought inhertiting meant that everything was inherited, including functions.

 

They are, and you don't have to define them again unless you're overriding the parent class' behavior. (or defining a virtual function, whatever)

 

It would make more sense for the parent class to load a bitmap from a path stored in a variable in the class (char *), and simply have child classes redefine that variable in their contructor. (Even if it's set in the parent class' constructor, the child class' constructor will be called after the parent class' is called.) Then simply create instances of your child class to create a new bitmap object with the custom bitmap (defined in the constructor of the child class).

  • Author
They are, and you don't have to define them again unless you're overriding the parent class' behavior. (or defining a virtual function, whatever)

 

It would make more sense for the parent class to load a bitmap from a path stored in a variable in the class (char *), and simply have child classes redefine that variable in their contructor. (Even if it's set in the parent class' constructor, the child class' constructor will be called after the parent class' is called.) Then simply create instances of your child class to create a new bitmap object with the custom bitmap (defined in the constructor of the child class).

Ok, when I get home, I'll try doing that.

All public and protected members are inherited. But, since i redefined the function, I had to redeclare it too.

If the 2 classes have nothing in common, don't inherit.

Usually, when you inherit you only EXTEND it's functionality. If you only redefine it's functionality, just rewrite everything.

  • Author
Never mind, I changed it all around, and included the HDC and HTBIMAP variables and functions for each child class itself (which it turns out I needed to do anyway), and it's working fine now.

It's a good idea to plan out your OOP stuff ahead of time, that way you can find any discrepancies or areas where poor design might affect your app. The fewer ugly hax you have to do (like directly accessing member variables of a class), the better, as problems generally tend to snowball as the app grows in size/complexity.

 

Glad to hear you sorted it out. :)

Guest
This topic is now closed to further replies.