I'll just refer to these in general terms for convenience; if I need to show more specific code, please let me know.
I have an abstract base class Parent, and a derived class Child. They are both friends with objects of Graphics type, in order for it to handle loading and displaying any Child's graphics at a given position using a sprite location in its pointer data member.
class Parent
{
friend class Graphics;
private:
Vector Position; // stores an X, Y, and Z value
Sprite* pSprite; // stores a pointer to the image to be drawn
};
class Child : public Parent
{
private:
Vector Position; // stores an X, Y, and Z value
Sprite* pSprite; // stores a pointer to the image to be drawn
};
class Graphics
{
public:
void Render(Parent* objectToDraw);
};
So say I have a Child object called "Character", and I pass it to the Graphics class as Render(&Character). Accessing pSprite with Character->pSprite in Render()'s functions doesn't give me any errors, but causes my program to freeze. Passing the object's members normally, by say, only having Render take a Child object rather than a pointer to its parent class, and accessing it by Character.pSprite, however, works perfectly fine. Obviously, I don't want to handle it this way because I'd have to overload Render() for each of my Child classes, so what's the problem I'm having? I thought there wasn't a difference from accessing a pointer to an object's member's by the arrow operator to just accessing a dereferenced object's members with the dot operator? Or is this some weird property of passed pointers-to-base-classes where I have to do something special with the object's pointer members?