Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Derived class' pointer data member freezes my program at use


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 Akashi   Members   -  Reputation: 268

Like
0Likes
Like

Posted 18 June 2013 - 12:54 PM

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?



Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 13693

Like
0Likes
Like

Posted 18 June 2013 - 01:00 PM

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.


Edited by Álvaro, 18 June 2013 - 01:01 PM.


#3 phil_t   Crossbones+   -  Reputation: 3955

Like
0Likes
Like

Posted 18 June 2013 - 01:01 PM

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.



#4 Akashi   Members   -  Reputation: 268

Like
0Likes
Like

Posted 18 June 2013 - 01:27 PM

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.


Edited by StoneMask, 18 June 2013 - 01:34 PM.


#5 belfegor   Crossbones+   -  Reputation: 2693

Like
1Likes
Like

Posted 18 June 2013 - 01:33 PM

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


Edited by belfegor, 18 June 2013 - 01:35 PM.


#6 Akashi   Members   -  Reputation: 268

Like
0Likes
Like

Posted 18 June 2013 - 01:36 PM

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



#7 Álvaro   Crossbones+   -  Reputation: 13693

Like
0Likes
Like

Posted 18 June 2013 - 01:49 PM


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.



#8 Akashi   Members   -  Reputation: 268

Like
0Likes
Like

Posted 19 June 2013 - 06:39 PM

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?



#9 Álvaro   Crossbones+   -  Reputation: 13693

Like
0Likes
Like

Posted 19 June 2013 - 07:05 PM

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.



#10 Akashi   Members   -  Reputation: 268

Like
0Likes
Like

Posted 19 June 2013 - 08:55 PM

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS