Avoiding redundant functions

C

Colin Kern

I'm less familiar with C than I am with C++, and right now I have a
problem with many structs I have defined where using C++ inheritance
classes would be perfect. For the functions that don't use any
variables unique to a specific species of this group of structs, is
there any way to avoid having to simply write multiple, almost
identical functions that only differ in the types of its arguements
and local variables?
 
L

Leor Zolman

I'm less familiar with C than I am with C++, and right now I have a
problem with many structs I have defined where using C++ inheritance
classes would be perfect. For the functions that don't use any
variables unique to a specific species of this group of structs, is
there any way to avoid having to simply write multiple, almost
identical functions that only differ in the types of its arguements
and local variables?

One way is a technique known as "discriminated unions". This is one way of
achieving run-time polymorphism, which may be more than you need, but it
may just end up being the least of several evils in this case.

On the other hand, if each struct you're working with has exactly the same
layout, you /may/ be able to get away with casting to enable a uniform
naming scheme. Without more specific details on your structs and what
you're trying to do with them, I can't really say.

Here's the example of discriminated unions, anyway, in case that's of any
use (and these all have a /different structure/):

#include <stdio.h>

enum { TYPE1, TYPE2, TYPE3 /* ,... */ };

struct type1 {
int x;
long c;
};

struct type2 {
double y;
};

struct type3 {
char a;
double b;
};

struct master {
int type;
union {
struct type1 t1;
struct type2 t2;
struct type3 t3;
/* ... */
} u;
};

typedef struct master master;

void process(master *m)
{
switch (m->type)
{
case TYPE1:
printf("type1: x = %d\n", m->u.t1.x);
break;
case TYPE2:
printf("type2: y = %f\n", m->u.t2.y);
break;
case TYPE3:
printf("type3: a = %c\n", m->u.t3.a);
break;
default:
printf("Bad type\n");
}
}

int main()
{
master m1, m2, m3;
m1.type = TYPE1;
m1.u.t1.x = 42;
m2.type = TYPE2;
m2.u.t2.y = 2.23606679774;
m3.type = TYPE3;
m3.u.t3.a = 'z';

process(&m1);
process(&m2);
process(&m3);
return 0;
}

Output:
type1: x = 42
type2: y = 2.236067
type3: a = z

-leor
 
E

E. Robert Tisdale

Colin said:
I'm less familiar with C than I am with C++,
and right now I have a problem with many structs I have defined
where using C++ inheritance classes would be perfect. For the
functions that don't use any variables unique to a specific species
of this group of structs, is there any way to avoid
having to simply write multiple, almost identical functions
that only differ in the types of its arguments and local variables?

No. You can't do this in C++ either.

typedef struct Base {
// ...
} Base;

void Base_f(const Base* pB);

typedef struct Derived {
Base B;
// ...
} Derived;

inline static const
Base* Derived_Base(const Derived* pD) {
return &(pD->B);
}

void Derived_f(const Derived* pD);

The C computer programming language does *not*
support inheritance so, if you want to pass a reference
to a Derived object

Derived d;

to Base_f, you must write

Base_f(Derived_Base(&d));
 
A

Anthony Wong

Colin Kern said:
I'm less familiar with C than I am with C++, and right now I have a
problem with many structs I have defined where using C++ inheritance
classes would be perfect. For the functions that don't use any
variables unique to a specific species of this group of structs, is
there any way to avoid having to simply write multiple, almost
identical functions that only differ in the types of its arguements
and local variables?

One possibility is to use macros to represent the argument types and change
the
definitions as needed.

--Anthony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top