Over-riding default methods in base classes

Started by
7 comments, last by SiCrane 18 years, 8 months ago
Hey got a problem, ive made a standard Object linked list, so i dont have to make a differnt list type for all of my object but having a problem getting the objects to draw which i think is because in Object i have an empty default Draw() method, and then a full Draw() method in each specific objects class. The problem is in my linked list drawing function i have to call object.draw() as opposed to say player.draw() which i think is resulting in the blank default method being called. Ive added a type to each of my objects so i can check what sort of object it is when the linked list methods are run, but how can i access an objects specific Draw() method?
Advertisement
What programming language is this? Assuming C++ the problem could be that you didn't declare your Draw() method to be virtual or you are storing Objects by value in your linked list which causes slicing.
Yup im using C++ and the Draw function is declared virtual as below:
#ifndef __OBJECT_H#define __OBJECT_H#define PI		3.14159#define RAD2DEG	57.2957795#define BULLET	1#define ENEMY	2#define PLAYER	3#include <windows.h>#include <gl/gl.h>#include <gl/glu.h>#include "Vector.h"class Object{public:	int TYPE;public:	Object() {};	~Object(){};	Vector CrossProduct(Vector v1, Vector v2, Vector v3)	{		Vector va =  v1 - v2;		Vector vb =  v2 - v3;		return (va ^ vb);	}	virtual void Draw()	{};	virtual void Update(float dt) {};	};#endif;


tried declaring it as virtual void Draw = 0; but get a load of errors of this kind:

error C2259: 'Object' : cannot instantiate abstract class due to following members:
object.h(18) : see declaration of 'Object'
objectlist.h(38) : warning C4259: 'void __thiscall Object::Draw(void)' : pure virtual function was not defined
object.h(35) : see declaration of 'Draw'
It sounds like you are storing your Objects by value in your list. Try switching to storing (smart) pointers to Object instead.

Also what is that CrossProduct() member function doing in your Object class?
Ok i dunno if i am or not so heres my linked list code too so you can tell me if i am lol. The crossproduct functions in there for working out polygon normals for my objects, just seemed like an easy place to put it :)

#ifndef __OBJECTLIST_H#define __OBJECTLIST_H#include <stdlib.h>#include <stdio.h>#include <windows.h>#include "object.h"// Set up node type for linked liststruct Node{			Object object;	Node *next;	Node *prev;};class ObjectList{public:	Node *head;	Node *tail;public:	ObjectList() 	{ 		head = NULL;		tail = NULL;		};		~ObjectList() { };	void addNode(Object object);    void Draw(float dt);	void deleteList();};#endif

#include "objectList.h"// *********************************// * Add a new node to the List// *********************************void ObjectList :: addNode(Object object){	if(head == NULL) // If first node to be inserted	{		head = new Node;		// New node		tail = head;						tail->next = NULL;			// No other nodes		tail->prev = NULL;	}	else	{		tail->next = new Node;		// New node		tail->next->prev = tail;	// Set new nodes prev pointer to the old tail		tail = tail->next;			// Move tail to end of list		tail->next = NULL;			// Set tails next pointer to NULL;	}	tail->object = object;			// Initialise new node with data}// **********************************// *Draw all Bullits in the List// **********************************void ObjectList :: Draw(float dt){	Node *thisNode = head;	while (thisNode != NULL)	{				thisNode->object.Update(dt);			thisNode->object.Draw();			// Draw current bullit			thisNode=thisNode->next;			// Move to next bullit	}		}// **********************************// * Delete list & free memory// **********************************void ObjectList :: deleteList(){	Node *thisNode = head;		Node *temp = NULL;	while (thisNode != NULL)	{		temp = thisNode;			// Copy current node to temp		thisNode = thisNode->next;	// Move to next node		delete temp;				// Delete node in temp	}	head = NULL;	// Reset pointers to NULL	tail = NULL;	//}
Yes you are storing the Object by value. Here is the offending piece of code:
struct Node{			Object object;	Node *next;	Node *prev;};

You want to change the Object object to Object * object or a smart pointer to Object.
Cheers mate, this is what happens when u spend 3 years learning java at uni ;) damn pointers lol
oh btw, whats a smart pointer when its at home?? Also think thats gonna give me problems as when i call the addNode method with a nameless object how am i spose to point to it? wouldnt that mean id have to decalre all the objects first which defeats the point of having a linked list?
Smart pointers are special classes that more or less act like pointers but add something extra. One of the most commonly used smart pointer classes, boost::shared_ptr, manages a reference count on a object to automatically delete the object when all references to the object are destroyed. This lets you not worry about deleting the object manually.

Even if you don't use a smart pointer, you can still use new to create the objects when you assign them to your linked list nodes.

This topic is closed to new replies.

Advertisement