Re: OOP



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>
.



Relevant Pages

  • [2.6 patch] drivers/video/: more cleanups
    ... static int aty128fb_check_var(struct fb_var_screeninfo *var, ... +#ifndef MODULE ... +static int __init aty128fb_init ... +static void radeon_save_state (struct radeonfb_info *rinfo, ...
    (Linux-Kernel)
  • Re: CPU recommendations?
    ... extern void startMotor; ... but MI (especially interface inheritance) ... Things like RTTI and exceptions give little value for money (or source code clarity or generated code quality), ... I've seen templates that provide neat and flexible ...
    (comp.arch.embedded)
  • Re: CPU recommendations?
    ... extern void startMotor; ... the interface definition. ... generated or used for templates, ... Interface inheritance - In this ...
    (comp.arch.embedded)
  • [PATCH 1/5] Sonypi: whitespace fixes
    ... * GNU General Public License for more details. ... -static inline void sonypi_initq{ ... +static inline unsigned char sonypi_pullq ... +static int sonypi_misc_fasync ...
    (Linux-Kernel)
  • [PATCH 14/29] kexec-kexec-generic
    ... * kernel binaries. ... +extern int machine_kexec_prepare(struct kimage *image); ... +extern void machine_kexec_cleanup; ... +static int do_kimage_alloc(struct kimage **rimage, unsigned long entry, ...
    (Linux-Kernel)