Derived class' pointer data member freezes my program at use

Started by
8 comments, last by StoneMask 10 years, 10 months ago

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?

Advertisement

It is probably an error that Child defines its own Position and pSprite members. They are already defined in the base class, so you don't need them there. As it is, your Child class has four members: Parent::Position, Parent::pSprite, Position and pSprite. Your runtime error is probably because you are setting one member and reading another.

Edit: Also, those members should probably be `protected' in the base class, and not `private', since you intend to use them in derived classes.

I'm not sure why your program would freeze, but you definitely have a problem here. You have duplicate declarations of Position and pSprite in Child. Those member are already inherited from Parent, so they don't need to be declared again (you can make them protected instead of private if you need to access them in subclasses).

Likely you were only assigning the pSprite in the child class and not the parent class, so your program crashed when accessing Parent's pSprite.

I was also confused at first, because I didn't have the duplicate declarations the first time around, and it gave me errors like this:

error C2248: 'Parent::Position': cannot access private member declared in class 'Parent'

I thought deriving publically solved that issue, or something. idk

EDIT: Okay, so I figured out I guess I'm supposed to have all the Parent class' members be protected rather than private. Can someone lay down the basics on me so I don't forget? Like, how exactly abstract classes work. For instance, declaring a class Child : (keyword) Parent. What exactly does that do? I get private, I get protected, and I get public, but I haven't run into a use for that keyword, as far as I know.

Set your base class data as protected to have access in derived class.

Yeah, thanks. I feel dumb lol. Setting the members to protected miraculously fixed my issue somehow.


Can someone lay down the basics on me so I don't forget? Like, how exactly abstract classes work. For instance, declaring a class Child : (keyword) Parent. What exactly does that do? I get private, I get protected, and I get public, but I haven't run into a use for that keyword, as far as I know.

You can think of inheriting from class X as being very close to having a member of type X, with some syntactic sugar added so you can refer to the members of X directly. When you cast a pointer to your class to a pointer to X, you are computing the address of the member of type X. Now, if the (keyword) you are asking is "public", "protected" or "private", it tells you whether that implicit member of type X is public, protected or private. If it says "virtual" it's trickier, but it basically means the implicit member is actually a pointer to X, and then there is some magic as to where the actual object of type X lives. I don't know all the details and I am happy not knowing them, because friends don't let friends use multiple inheritance, and if you don't use multiple inheritance there is no need for virtual inheritance.

Alright, so I guess I kind of understand the that I don't need to know everything about it, but what do you mean by multiple inheritance? Like, a base class deriving from a base class? And in this specific case, are you saying I don't have to use an abstract base class?

Alright, so I guess I kind of understand the that I don't need to know everything about it, but what do you mean by multiple inheritance? Like, a base class deriving from a base class? And in this specific case, are you saying I don't have to use an abstract base class?

No, multiple inheritance means a class deriving from two or more classes (you should learn how to search the web). I wasn't saying anything about your specific case.

Yeah, sorry. I treat these more as conversations than as help topic responses, as I should. Thank you for your help.

This topic is closed to new replies.

Advertisement