Archived

This topic is now archived and is closed to further replies.

Virtual function help!

This topic is 5841 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites