Register
Site Login
Site Search
Forums
Advertisement
Welcome to PocketMatrix. PocketMatrix is dedicated to providing the best online community for mobile device developers and enthusiests. What's new?

POO structures & sub classes


POO structures & sub classes

Postby Lewil » Sep 11, 2002 @ 3:52pm

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
User avatar
Lewil
pm Member
 
Posts: 108
Joined: Apr 25, 2002 @ 1:39pm


Postby Rhino123 » Sep 12, 2002 @ 10:49am

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: Select all









10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
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
20 lines; 10 keywds; 0 nums; 15 ops; 2 strs; 0 coms    Syntactic Coloring v0.4 - Dan East  


Code: Select all









10 
11 
12 
13 
myclass.cpp

#include <myclass.h>

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

myclass::~myclass()
{
}
etc...
13 lines; 1 keywds; 2 nums; 27 ops; 0 strs; 0 coms    Syntactic Coloring v0.4 - Dan East  


hope that helped!

Rhino
User avatar
Rhino123
pm Member
 
Posts: 66
Joined: Jul 2, 2002 @ 2:58pm
Location: Amsterdam


Postby Rhino123 » Sep 12, 2002 @ 1:29pm

hmmm looking at it again,

#include <myclass.h>

should read

#include "myclass.h"

of course :)

R
User avatar
Rhino123
pm Member
 
Posts: 66
Joined: Jul 2, 2002 @ 2:58pm
Location: Amsterdam


Postby Lewil » Sep 13, 2002 @ 1:37pm

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;

?!?
User avatar
Lewil
pm Member
 
Posts: 108
Joined: Apr 25, 2002 @ 1:39pm


Postby BurningSheep » Sep 13, 2002 @ 2:16pm

That should be a pointer

Surface *m_mysurface;
Do you want custom Snails levels? Click here
User avatar
BurningSheep
pm Insider
 
Posts: 1226
Joined: Apr 12, 2002 @ 11:49pm
Location: The Netherlands


Postby Lewil » Sep 13, 2002 @ 2:19pm

oh ! :')

thanks !!
User avatar
Lewil
pm Member
 
Posts: 108
Joined: Apr 25, 2002 @ 1:39pm


Postby Lewil » Sep 13, 2002 @ 2:32pm

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 ?
User avatar
Lewil
pm Member
 
Posts: 108
Joined: Apr 25, 2002 @ 1:39pm


Postby Rhino123 » Sep 13, 2002 @ 4:14pm

ehm no... :wink:

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

it should read:

delete m_Surface


Reinier
User avatar
Rhino123
pm Member
 
Posts: 66
Joined: Jul 2, 2002 @ 2:58pm
Location: Amsterdam


another pitfall

Postby draklava » Sep 17, 2002 @ 11:55pm

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: Select all





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

MyObj* myobj_array = new MyObj[5];
delete [] myobj_array;
5 lines; 4 keywds; 1 nums; 14 ops; 0 strs; 0 coms    Syntactic Coloring v0.4 - Dan East  


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
User avatar
draklava
pm Member
 
Posts: 79
Joined: May 15, 2002 @ 6:13am
Location: Atlanta, Albania, Wherever


Return to Phantom's Forum


Sort


Forum Description

Discuss any of Phantom's projects here (Operation Nutcracker, etc.)

Moderators:

sponge, RICoder, Phantom

Forum permissions

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum