473,772 Members | 2,510 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Very Simple, Minimalist Technique For OOP in C...

Here is the example code:

- http://appcore.home.comcast.net/vzoo.../interface.zip
which is an analog of the following technique:

- http://groups.google.com/group/comp....f857051ca4029b
It's definitely not full-blown OOP is any sense of the term, however imho, I
think it could be fairly useful in certain scenarios... Now, let me try to
briefly describe it:

The provided example shows how a minimalist abstract interface for a "shape"
object might look. It also includes an implementation of simple circle and
square objects that are compatible with the abstract shape interface. If you
want to try and add another shape to get a feel for how "cumbersome " the
interface method is, well, here is a quick example of that:
<pseudo-code for adding, lets say, a triangle>
_______________ ___________

triangle.h
_______________ ___
/* Put include guard here */

#include "ishape.h"

/* Return values that are non-zero indicate error's */

/* Create a new triangle */
extern int Triangle_Create (IShape_t* const, /* [triangle params] */);

triangle.c
_______________ ___
#include "triangle.h "
typedef struct Triangle_s Triangle_t;

struct Triangle_s {
/* [triangle members] */
};
static int Triangle_IObjec t_Destroy(void* const);
static int Triangle_IObjec t_Status(void* const);
static int Triangle_IShape _Draw(void* const);
/* Triangle IShape Interface VTable */
static IShape_VTable_t const Triangle_IShape _VTable = {
Triangle_IObjec t_Destroy,
Triangle_IObjec t_Status,
Triangle_IShape _Draw
};
/* Triangle Interface Functions */
int
Triangle_Create (
IShape_t* const IThis,
/* [triangle params] */) {
Triangle_t* const This = malloc(sizeof(* This));
if (This) {
/* [init This triangle] */
IOBJECT_INIT(IT his, This, &Triangle_IShap e_VTable);
return 0;
}
return -1;
}
/* Triangle IObject Interface Functions */
int
Triangle_IObjec t_Destroy(void* const ThisState) {
Triangle_t* const This = ThisState;
free(This);
return 0;
}
int
Triangle_IObjec t_Status(void* const ThisState) {
Triangle_t* const This = ThisState;
/* [determine This triangle state; this is optional] */
return 0;
}
/* Triangle IShape Interface Functions */
int
Triangle_IShape _Draw(void* const ThisState) {
Triangle_t* const This = ThisState;
/* [draw This triangle] */
return 0;
}
_______________ ___________


IMHO, that should be "fairly" straight forward... Now, here is how you could
use your Triangle:
_______________ ___________

#include "triangle.h "

int Example(void) {
IShape_t MyShape;

int rStatus = Triangle_Create (&MyShape, /* [triangle params] */);
if (! rStatus) {

rStatus = IShape_Draw(&My Shape);
if (rStatus) {

rStatus = IObject_Status( &MyShape);
}

rStatus = IObject_Destroy (&MyShape);
}

return rStatus;
}
_______________ ___________
I was wondering if you have come across any superior methods for very basic
OOP in C?
Any thoughts? Is this method total crap?

;^)
Thanks.

Jun 21 '07
10 2250
Chris Thomasson wrote:
"Malcolm McLean" <re*******@btin ternet.comwrote in message
news:kZ******** *************@b t.com...
>>
"Chris Thomasson" <cr*****@comcas t.netwrote in message
news:h_******* *************** ********@comcas t.com...
>>I was wondering if you have come across any superior methods for very
basic OOP in C?
[...]
>However it really is a lot of trouble. The syntactical support isn't
really there, so code quickly becomes a complete mess.

Yeah, that seems to attempt to support more OOP techniques than the
"minimalist " method posted, which only provides an abstract interface
technique.
You are right, the syntactical support isn't really there, but it is
quite possible to do full OOP in C with virtual functions and
everything, and it isn't that hard to do really.
It is even possible to do it better than in C++ with respect to using an
object oriented plugin system, something not easily done in C++, at
least not if you are *not* interested in recompiling every plugin at any
change in some of the base classes. I do not know about C# or other
languages - it might be better solved there but C is somewhat easier
because everything becomes visible and that makes it easier to see the
limitations and understand what happens. (I have done two
implementations of plugin based object oriented systems, one in C++ and
later one in C, the one in C actually meets the specifications better
even if it is more lines of code to do the work).
Jun 24 '07 #11

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

Similar topics

42
9745
by: Steven O. | last post by:
I am seeking some kind of tool that I can use for GUI prototyping. I know how to use Visual Basic, but since a lot of software is being coded in Java or C++, I'd like to learn a Java or C++ -based tool. Back when I took my Java and C++ classes (two or three years ago), the available tools -- at least the ones I could find -- were still not as easy, not as "drag-and-drop", as Visual Basic. Has that changed? Is there some software out...
8
6088
by: Mark | last post by:
Hi, I'm looking for some ideas on how to build a very simple Event processing framework in my C++ app. Here is a quick background ... I'm building a multithreaded app in C++ (on Linux) that uses message queues to pass pointers to Events between threads. In my app there are simple events that can be defined using an enum (for example an event called NETWORK_TIMEOUT) and more complex events that contain data (for example an event called...
11
17574
by: DJJ | last post by:
I am using the MySQL ODBC 3.51 driver to link three relatively small MySQL tables to a Microsoft Access 2003 database. I am finding that the data from the MySQL tables takes a hell of a long time to load making any kind linkage with my Access data virtually useless. I have the MySQL driver setup in as a USER DSN. The MySQL data is sitting out on a server and the Access database is running locally. The network connection is very...
3
16779
by: Douwe | last post by:
I try to build my own version of printf which just passes all arguments to the original printf. As long as I keep it with the single argument version everything is fine. But their is also a version which uses the "..." as the last parameter how can I pass them to the orignal printf ? void myprintf(char *txt, ...) printf(txt, ???????); }
12
20704
by: John | last post by:
I can't get my head around this! I have the following code: <% .... Code for connection to the database ... .... Code for retrieving recordset ... If Not rs.EOF Then ... Do something...
14
5617
by: rtk | last post by:
I'm looking for information on building a tiny/small/minimalist/ vanilla python interpreter. One that implements the core language and a few of the key modules but isn't tied to any specific operating system. I guess I'm asking for the smallest subset of the standard Python source code files that is necessary to get a working interpreter using a plain C compiler. Is this even possible? If so, has someone done it already? I've
112
4760
by: Prisoner at War | last post by:
Friends, your opinions and advice, please: I have a very simple JavaScript image-swap which works on my end but when uploaded to my host at http://buildit.sitesell.com/sunnyside.html does not work. To rule out all possible factors, I made up a dummy page for an index.html to upload, along the lines of <html><head><title></title></ head><body></body></html>.; the image-swap itself is your basic <img src="blah.png"...
126
4428
by: jacob navia | last post by:
Buffer overflows are a fact of life, and, more specifically, a fact of C. All is not lost however. In the book "Value Range Analysis of C programs" Axel Simon tries to establish a theoretical framework for analyzing C programs. In contrast to other books where the actual technical difficulties are "abstracted away", this books tries to analyze real C programs taking into account pointers, stack frames, etc.
17
5819
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /* Simple Thread Object ______________________________________________________________*/ #include <pthread.h> extern "C" void* thread_entry(void*);
0
9621
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7461
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2851
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.