PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

POO structures & sub classes

POO help request again ...

i use my own classes in my game

i have these classes defined in external myclass.h and codes in a separate myclass.cpp

ok that work well till here

if i want the methods of my classes to access some Api functions such as PlaySample() or let's say m_Display->any_drawing_method() or even declare a Surface() in my class to have all the overloaded standard methods, what DO i have to put in the myclass.h or in the header of myclass.cpp for it to work ?

... exemple:
i have a class which consists of a Sprite + some variables + some methods and i'd like it to be defined in external .cpp and .h
hi Lewil,

Not sure that I understood correctly. You want to know what you have to declare in a separate sourcefile in order for it to use the overloaded lib classes/api?

if so do this:

Code:

myclass.h

#ifndef _MYCLASS_H
#define _MYCLASS_H

#include "Framegame.h"
#include "overloaded.h"

using namespace Overloaded;

class myclass
{
public:
myclass();
~myclass();

private:
Surface m_Surface;
};
#endif


Code:

myclass.cpp

#include <myclass.h>

myclass::myclass()
{
m_Surface = g_Factory->NewSurface(16, 16);
}

myclass::~myclass()
{
}
etc...


hope that helped!

Rhino
hmmm looking at it again,

#include <myclass.h>

should read

#include "myclass.h"

of course :)

R
ok Rhino, that's what i'm trying, but i always have a :

error C2259: 'Surface' : cannot instantiate abstract class due to following members: ...\v0.99b - july 29th, 2002\overloaded\headers\surface.h(22) : see declaration of 'Surface'

on the line in header:

Surface m_mysurface;

?!?
BurningSheep · 13 Sep 2002, 13:16 UTC
That should be a pointer

Surface *m_mysurface;
oh ! :')

thanks !!
ok seems to work

i just have a memory leak, i think, in the end (deallocation)
do i have to free that surface in the ~myclass ?

i tried
free( m_surface )
free( *m_surface )

(with scoped ptr it would be free ( m_surface.Get() ) ? )

... later ...
oh ! a:
m_surface->~Surface()
works
is it ok ?
ehm no... :wink:

never call the destructor yourself unless you know what you are doing

it should read:

delete m_Surface


Reinier
draklava · 17 Sep 2002, 22:55 UTC — another pitfall
Lewil

one other thing to keep track of is when you have an array of objects.

The rule of thumb is to always call delete the same way you called new

Code:

MyObj* myobj = new MyObj();
delete myobj;

MyObj* myobj_array = new MyObj[5];
delete [] myobj_array;


notice the second example has empty brackets after delete. This is to ensure you clean up all elements of the array. If you don't include the brackets you will only be deleting the first element of the array and the other elements will still be hanging around