• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
StoneMask

Derived class' pointer data member freezes my program at use

9 posts in this topic

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?

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites


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.

0

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0