Skip to main content
gamedev.net
🔒 Locked

Virtual function help!

Started by BradDaBug Dec 14, 2001 at 8:07 PM 5 replies 400+ views
Original Post
BradDaBug
BradDaBug
I''m trying to write a GUI. Each GUI object, like buttons, windows, whatever, are specific classes that are all derived from a base GUIObject class. I keep track of these object with a linked list full of pointers to the class, but they''re all typecast as just pointers to the base class the classes are all derived from. The base class has a virtual function Draw(). as well as all the other classes. I want it to go through the list and draw all the objects. Heres some code: while(tempnode != NULL) { tempnode->ptr->Draw(); tempnode = tempnode->next; } This is the meat of the transversal. tempnode is the current node we''re working with, ptr is a pointer to the base class which should be the same thing (should point to the same thing) as the main class for whatever particular type of object we''re working with, and Draw() is the virtual function in question. The problem, I get a nice crash when it tries to call this Draw() function. What''s going on?
I like the DARK layout!
23yrold3yrold
23yrold3yrold
Yes, make sure your linked list code is impeccable. Better yet, use the STL''s class list (that''s how I did my GUI).

Chris Barry
Jesus saves ... the rest of you take 2d4 fire damage.
BradDaBug
BradDaBug
Ok, I''ve done some debuggin, and I''ve discovered for some reason tempnode->ptr is NULL in the drawing function.

The strange thing is, I''m using almost the same exact code in FindControl() as I am the code to draw everything. The only difference is FindControl() does a strcmp() with tempnode->ptr->Name and the drawing function does tempnode->ptr->Draw().

FindControl() works perfectly! I don''t understand why the drawing function is screwing up!
I like the DARK layout!
BradDaBug
BradDaBug
Holy flaming animals, I think I have figured it out! How could I have been so stupid!

I was creating the controls in the function InitGame()! The drawing function was being called in GameMain()! The controls were going out of scope!

AAAAAAAAHHHHHHH!!!!!! Why is it always something so stupid?
I like the DARK layout!
BradDaBug
BradDaBug
Wait a minute, its still not working. I created created some functions that did malloc() to create the controls.

The controls exist and all, but its still crashing at the controls Draw() function!

I wrote a smaller console app to figure out what is going on, and its doing the same thing. Heres the class definitions:

class BaseClass
{
public:
virtual void WriteText(void);
};

class Lego : public BaseClass
{
public:
virtual void WriteText(void);
};

class Toy : public BaseClass
{
public:
virtual void WriteText(void);
};

Heres the main code:

include
#include "main.h"


BaseClass* base;
Lego* legos;
Toy* toys;


main()
{

printf("Calling function...\n");

base->WriteText();
legos->WriteText();
toys->WriteText();


return(NULL);
}

void BaseClass::WriteText(void)
{

printf("Called BASECLASS! \n");
}

void Lego::WriteText(void)
{
printf("Called Lego!\n");
}

void Toy::WriteText(void)
{
printf("called toys!\n");
}

It crashes when it tries to call WriteText(). Why?

I like the DARK layout!
Red Ant
Red Ant
Because you never actually created an object that each of the pointers refers to. Add this to your code.

  
base = new BaseClass;
legos = new Lego;
toys = new Toy;

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.