473,387 Members | 1,585 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,387 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 1537
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.