delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/09/03/12:49:55

From: "Mr Jinks" <lcdvt AT worldonline DOT nl>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Functions in struct's... possible? How?
Date: 3 Sep 1997 14:23:24 GMT
Organization: World Online
Message-ID: <01bcb874$f70442e0$b986f1c3@lodewijk>
References: <33FCDA5C DOT 2353659F AT execulink DOT com> <5tippg$ci7$2 AT news DOT sendit DOT nodak DOT edu> <5tkq9a$2se$1 AT helios DOT crest DOT nt DOT com> <5tmcai$nuo$1 AT news DOT sendit DOT nodak DOT edu> <340B1E27 DOT 44FAD207 AT alcyone DOT com> <01bcb756$1d6d3ce0$5386f1c3 AT lodewijk> <5uisg2$s96 AT freenet-news DOT carleton DOT ca>
NNTP-Posting-Host: dlft1-p185.worldonline.nl
Mime-Version: 1.0
Lines: 132
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

// object.h

#ifndef OBJECT_H
#define OBJECT_H

#define BASE_OBJECT      \
  void (*DoSomething)();

typedef struct { BASE_OBJECT } Object;

typedef struct
{
  BASE_OBJECT
  int x;
}X_Object;

typedef struct
{
  BASE_OBJECT
  int y;
}Y_Object;

extern void InitializeObjects(void);   //Declared in initobj.c
extern void Manip_X(X_Object *);             //Declared in manip_x.c
extern void Manip_Y(Y_Object *);             //Declared in manip_y.c
extern Object *ObjectList[];           //Declared in example.c

#endif 

// eof object.h

// initobj.c


#include <stdlib.h>
/* Include file which contains all object functions and structures */
#include "object.h"

void InitializeObjects()
{
  X_Object * x;
  Y_Object * y;

  if((x = malloc(sizeof(X_Object))) == NULL)
  {
    printf("Couldn't get memory\n");
    exit(1);
  }
  x->DoSomething = Manip_X;

  if((y = malloc(sizeof(Y_Object))) == NULL)
  {
    printf("Couldn't get memory\n");
    exit(1);
  }
  y->DoSomething = Manip_Y;

  ObjectList[0] = (Object*)x;
  ObjectList[1] = (Object*)y;
}

// eof initobj.c

// manip_x.c 

#include <stdio.h>
/* Include file which contains all object functions and structures */
#include "object.h"

void Manip_X(X_Object * x)
{
  /* Set variable x from a X_Object to 1 */
  x->x = 1;

  /* Display the value x from the X_Object */
  printf("X_Object int x = %d\n",x->x);
};

// eof manip_x.c

// manip_y.c 

#include <stdio.h>
/* Include file which contains all object functions and structures */
#include "object.h"

void Manip_Y(Y_Object * y)
{
  /* Set variable y from a Y_Object to 2 */
  y->y = 2;

  /* Display the value y from the Y_Object */
  printf("Y_Object int y = %d\n",y->y);
}

// eof manip_x.c

// example.c

/* Include file which contains all object functions and structures */
#include "object.h"

/* Global list of basic objects */
Object *ObjectList[2];

int main()
{
  int i;

  /* Basic object pointer, that you will use to manipulate all objects */
  Object * ObjectPtr;

  /* Initialize all objects here */
  InitializeObjects();

  /* Example loop which manipalates all objects */
  for( i = 0; i < 2; i++)
  {
      ObjectPtr = ObjectList[i];
      ObjectPtr->DoSomething(ObjectPtr);
  /*  OUTPUT:

      X_Object int x = 1
      Y_Object int y = 2

  */
  }

  return 0;
}

// eof example.c

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019