Re: OOP
- From: Tor Rustad <bwzcab@xxxxxxxxx>
- Date: Mon, 30 Jun 2008 19:21:33 +0200
pete wrote:
campyhapper@xxxxxxxxx wrote:Hi folks,
I tend to prefer C, and of course I know that structs
can be used in C to achieve something like
an object-oriented design. And I prefer C in part
because C++ has, I think, grown into a bit of a
monster wherein readability is sacrificed.
But I wonder, has anyone ever tried to create a
sort of lite version of C++, a C+ if you will, that adds
to C just a few key features and disallows things
like templates, multiple inheritance and the like?
I got the e_type interface for sorting functions from Dann Corbit.
It's a step in the direction towards templates. I like it.
All you have to do is
#define E_TYPE /* array element type */
and
#define GT(A, B) /* a "Greater Than" macro */
and you can sort a one dimensional array of any type.
Interesting, this reminds me of the ADT trick used by Sedgewick, in his "Algorithms in C" books, where an algorithm implementation could look like this:
#include <stdlib.h>
#include "Item.h"
#include "STACK.h"
static Item *s;
static int N;
void STACKinit(int maxN)
{ s = malloc(maxN*sizeof(Item)); N = 0; }
int STACKempty()
{ return N == 0; }
void STACKpush(Item item)
{ s[N++] = item; }
Item STACKpop()
{ return s[--N]; }
while the user write an <Item.h> and call the generic algorithms like this:
#include <stdio.h>
#include <string.h>
#include "Item.h"
#include "STACK.h"
main(int argc, char *argv[])
{ char *a = argv[1]; int i, N = strlen(a);
STACKinit(N);
for (i = 0; i < N; i++)
{
if (a[i] == ')')
printf("%c ", STACKpop());
if ((a[i] == '+') || (a[i] == '*'))
STACKpush(a[i]);
if ((a[i] >= '0') && (a[i] <= '9'))
printf("%c ", a[i]);
}
printf("\n");
}
--
Tor <echo bwzcab@xxxxxxxxx | tr i-za-h a-z>
.
- Follow-Ups:
- Re: OOP
- From: pete
- Re: OOP
- Prev by Date: Re: Fast fmodf(..., 1.0f)
- Next by Date: Re: problem passing pointer array
- Previous by thread: Re: OOP
- Next by thread: Re: OOP
- Index(es):
Relevant Pages
|