473,385 Members | 1,427 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,385 software developers and data experts.

Simulating inheritance and non-member functions in C

Hi Everyone,

I have been playing aroung with 'object-oriented' C for a while now
and I have come up with an interesting way to simulate C++ inheritance
and non-member functions in C in a 100% typesafe way. The object
derivation is a bit cloudy, but the code is fairly repetitive and
doesn't seem to be terribly error prone. Maybe some clever macros
could clean up the repetitive code a bit.

Can any of you please comment on possible non-obvious pitfalls from
using this approach? All comments are welcome. Espesially clever
macros that can reduce the amount or repetitiveness in the code.
Or maybe you could tell what is wrong or dangerous with this approach.

Thanks very much,

Doug Eleveld

/* The base object 'class'------------------------- */
typedef struct _object {
/* Some variables */
} _object;

typedef struct object {
/* Why use a union will become clear later */
union {
_object _object;
} base;
} object;

/* A convenience macro for getting a object base class from a derived class */
#define OBJECT(obj) &((obj)->base._object)

/* Some example functions and convenience macros for objects ------------ */
void _attach(_object * obj, _object * parent)
{
/* Do something special here */
(void) obj;
(void) parent;
}
#define attach(obj,par) _attach(OBJECT(obj), OBJECT(par))

void _detach(_object * obj)
{
/* Do something special here */
(void) obj;
}
#define detach(obj) _detach(OBJECT(obj))

/* Deriving a button class from the object -------- */
typedef struct _button {
/* This is the base class we 'derive from */
union {
_object _object;
} base;

/* Some variables */
int selected;
} _button;

typedef struct button {
union {
_button _button;
_object _object; /* Here we list all the 'public' base classes */
} base;
} button;

/* A convenience macro for getting a button base class from a derived class */
#define BUTTON(btn) &((btn)->base._button)

/* Some example functions and convenience macros for buttons ------------- */
void _select(_button * btn)
{
btn->selected = 1;
}
#define select(btn) _select(BUTTON(btn))

void _deselect(_button * btn)
{
btn->selected = 0;
}
#define deselect(btn) _deselect(BUTTON(btn))

int main (void)
{
object myobject, yourobject;
button mybutton;

/* We can call 'non-member' functions on object */
/* This is typesafe */
attach(&myobject, &yourobject);
detach(&myobject);
detach(&yourobject);

/* We can call button functions for button */
/* This is typesafe */
select(&mybutton);

/* We can also call base class functions on button */
/* This is typesafe */
attach(&mybutton, &myobject);

return 0;
}
Nov 13 '05 #1
3 2239
On Thu, 23 Oct 2003 22:45:18 UTC, de******@dds.nl (Doug Eleveld)
wrote:
Hi Everyone,

I have been playing aroung with 'object-oriented' C for a while now
and I have come up with an interesting way to simulate C++ inheritance
and non-member functions in C in a 100% typesafe way. The object
derivation is a bit cloudy, but the code is fairly repetitive and
doesn't seem to be terribly error prone. Maybe some clever macros
could clean up the repetitive code a bit.

Can any of you please comment on possible non-obvious pitfalls from
using this approach? All comments are welcome. Espesially clever
macros that can reduce the amount or repetitiveness in the code.
Or maybe you could tell what is wrong or dangerous with this approach.

Thanks very much,

Doug Eleveld

/* The base object 'class'------------------------- */
typedef struct _object {
/* Some variables */
} _object;
violating the namespace of the implementation.
typedef struct object {
/* Why use a union will become clear later */
union {
_object _object;
Hm, is your implemenation really happy with names starting with
underscore? You are sure that this name will not be used by the next
revision of it?
} base;
} object;

/* A convenience macro for getting a object base class from a derived class */
#define OBJECT(obj) &((obj)->base._object)

/* Some example functions and convenience macros for objects ------------ */
void _attach(_object * obj, _object * parent)
{
/* Do something special here */
(void) obj;
(void) parent;
}
#define attach(obj,par) _attach(OBJECT(obj), OBJECT(par))

void _detach(_object * obj)
{
/* Do something special here */
(void) obj;
a null statement lives as null statement independant of how many casts
you writes.
}
#define detach(obj) _detach(OBJECT(obj))

/* Deriving a button class from the object -------- */
typedef struct _button {
/* This is the base class we 'derive from */
union {
_object _object;
} base;

/* Some variables */
int selected;
} _button;
Another time you violates the namespace of your implementation.
typedef struct button {
union {
_button _button;
_object _object; /* Here we list all the 'public' base classes */
} base;
} button;

/* A convenience macro for getting a button base class from a derived class */
#define BUTTON(btn) &((btn)->base._button)

/* Some example functions and convenience macros for buttons ------------- */
void _select(_button * btn)
{
btn->selected = 1;
}
#define select(btn) _select(BUTTON(btn))

void _deselect(_button * btn)
{
btn->selected = 0;
}
#define deselect(btn) _deselect(BUTTON(btn))
It's bad style to define macros as all lowercase. All uppercase is
standard.

int main (void)
{
object myobject, yourobject;
There is no type object as you've ony defined type _object.
button mybutton;
You've neither typedef#d nor defined type button. You should never
violate the namespace of your application, then you would hoenly avoid
mistypes as such.

/* We can call 'non-member' functions on object */
/* This is typesafe */
attach(&myobject, &yourobject);
detach(&myobject);
detach(&yourobject);

/* We can call button functions for button */
/* This is typesafe */
select(&mybutton);

/* We can also call base class functions on button */
/* This is typesafe */
attach(&mybutton, &myobject);

return 0;
}

--
Tschau/Bye
Herbert

eComStation 1.1 Deutsch wird jetzt ausgeliefert!
Nov 13 '05 #2
On Fri, 24 Oct 2003 17:23:02 +0000, The Real OS/2 Guy wrote:
On Thu, 23 Oct 2003 22:45:18 UTC, de******@dds.nl (Doug Eleveld)
wrote:
#define deselect(btn) _deselect(BUTTON(btn))


It's bad style to define macros as all lowercase. All uppercase is
standard.


Quick, somebody tell the C standards commitee. Not only did they
forget to standardize this rule, they've broken it themselves.
int main (void)
{
object myobject, yourobject;


There is no type object as you've ony defined type _object.


You must have missed this part:
typedef struct object {
/* Why use a union will become clear later */
union {
_object _object;
} base;
} object; button mybutton;


You've neither typedef#d nor defined type button.


You must have missed this part:
typedef struct button {
union {
_button _button;
_object _object; /* Here we list all the 'public' base classes */
} base;
} button;


Nov 13 '05 #3
On Fri, 24 Oct 2003 14:29:07 -0400, in comp.lang.c , Sheldon Simms
<sh**********@yahoo.com> wrote:
On Fri, 24 Oct 2003 17:23:02 +0000, The Real OS/2 Guy wrote:
On Thu, 23 Oct 2003 22:45:18 UTC, de******@dds.nl (Doug Eleveld)
wrote:
#define deselect(btn) _deselect(BUTTON(btn))


It's bad style to define macros as all lowercase. All uppercase is
standard.


Quick, somebody tell the C standards commitee.


That was "standard" with a small s, not a capital S. Perhaps
"recommended practice" is a better word.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #4

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

Similar topics

10
by: jeffc | last post by:
When compiling the following program, I get an error on the line specified: The base class "B" cannot be initialized because it does not have a default constructor. If I remove the "virtual"...
15
by: Sinex | last post by:
Hi, Why does C# disallow multiple inheritance? Whats the reason behind this? Is there any advantage or is it just a method to avoid some problems (if so, what problems?) that come with multiple...
0
by: blackdevil1979 | last post by:
Hello, > "In COM, multiple inheritance between interfaces is not supported. However, by using the derived members capability, multiple inheritance can be simulated."..MSDN Library I have...
2
by: Kevin Newman | last post by:
I have been playing around with a couple of ways to add inheritance to a JavaScript singleton pattern. As far as I'm aware, using an anonymous constructor to create a singleton does not allow any...
0
by: Terry Hancock | last post by:
I've been discussing PyProtocols with a a friend collaborating with me on a SF game project, about the benefits and design concept of "component architecture", and I'm a little confused by what...
4
by: Roger Withnell | last post by:
I would like to freeze column and row headings on a webpage, simulating freeze panes as in an Excel spreadsheet. Don't seem to be able to do it with Frames. Is there a way with Javascript...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
6
by: Jackson | last post by:
I've got an inheritance question and was hoping brighter minds could guide me. I am in the strange situation where some of the methods in a subclass are actually more general than methods in a...
3
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is...
7
by: =?Utf-8?B?VG9tIEJvbWJhZGls?= | last post by:
Question: Can an outer non-equijoin be simulated using LINQ? It's quite unfortunate that LINQ doesn't support literal joins based on anything other than equality. I know that this the most common...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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: 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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.