main.h:
#if !defined(MAIN_H)
#define MAIN_H
struct Lemming {
int x;
int y;
};
void allocate_lemmings();
void deallocate_lemmings();
#endif
functions.cpp:
#include "main.h"
struct Lemming * globalLemmingsPointer = 0;
void allocate_lemmings() {
int i;
if (globalLemmingsPointer == 0) {
globalLemmingsPointer = new struct Lemming[100];
for (i = 0; i < 100; i++) {
globalLemmingsPointer[i].x = 0;
globalLemmingsPointer[i].y = 0;
}
}
}
void deallocate_lemmings() {
if (globalLemmingsPointer != 0) {
delete[] globalLemmingsPointer;
globalLemmingsPointer = 0;
}
}
main.cpp:
#include "main.h"
extern struct Lemming * globalLemmingsPointer;
int main (int argc, char ** argv) {
allocate_lemmings();
globalLemmingsPointer[10].x = 10;
globalLemmingsPointer[10].y = 10;
deallocate_lemmings();
return 0;
}
Indlæg senest redigeret d. 11.08.2008 13:39 af Bruger #2695