473,326 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Avoiding redundant functions

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?
Nov 14 '05 #1
3 1536
On 9 Apr 2004 17:02:11 -0700, ke****@earlham.edu (Colin Kern) wrote:
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
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #2
Colin Kern wrote:
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));

Nov 14 '05 #3

"Colin Kern" <ke****@earlham.edu> wrote in message
news:be*************************@posting.google.co m...
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
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Dan | last post by:
Quick question about passing variables to subs to reduce the need for publicly declared variables in VB6. If I have an event sub (MouseDown) that runs a few lines of code, how can I use a variable...
10
by: Nick Craig-Wood | last post by:
I'm trying to avoid using shell metacharacters in os.popen in a portable fashion. os.popen() only seems to take a string as the command which would need tricky quoting. os.popen2() can take a...
16
by: max(01)* | last post by:
hi everybody. suppose that code-1.py imports code-2.py and code-3.py (because it uses names from both), and that code-2.py imports code-3.py. if python were c, code-1.c should only *include*...
0
by: Victor Engmark | last post by:
When my XML Schema contains <xsd:sequence> <xsd:element name="id" type="slt_code"/> </xsd:sequence> , I don't want to duplicate this information by having to type <xf:bind id="slotId-bind"...
3
by: Colin Kern | last post by:
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...
10
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: šššššššštypedefšstructš{ ššššššššššššššššintššššši; ššššššššššššššššcharššššname; šššššššš}šMY_TYPE; ššššššššconstšcharš*
40
by: Neo The One | last post by:
I think C# is forcing us to write more code by enforcing a rule that can be summarized as 'A local variable must be assgined *explicitly* before reading its value.' If you are interested in what...
3
by: rickeringill | last post by:
Hi comp.lang.javascript, I'm throwing this in for discussion. First up I don't claim to be any sort of authority on the ecmascript language spec - in fact I'm a relative newb to these more...
5
by: Matthew Wilson | last post by:
I wrote some code to create a user and update a user on a remote box by sending emails to that remote box. When I was done, I realized that my create_user function and my update_user function were...
2
by: mde | last post by:
I'm wondering if there is a "best practice" for *creating doctests in methods* that reduces clutter/duplication of dummy instantiations. In the following code there are five (labeled 1-5) possible...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shζllξpτpο 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.