473,770 Members | 2,519 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding the ability to add functions into structures?

So structures are useful to group variables, so you can to refer to a
collection as a single entity. Wouldn't it be useful to also have the
ability to collect variable and functions?

Ask K&R say, C programs consist of variables to store the input and
functions to manipulate them.

This would make C object-oriented - how cool would that be?

Are there any problems with adding the ability to have functions
encapsulated in structures?

Dec 31 '05 #1
47 3893
In article <11************ **********@z14g 2000cwz.googleg roups.com>,
Albert <al************ *****@gmail.com > wrote:
So structures are useful to group variables, so you can to refer to a
collection as a single entity. Wouldn't it be useful to also have the
ability to collect variable and functions? Are there any problems with adding the ability to have functions
encapsulated in structures?


structures are data, functions are code. Having the ability to store
functions (instead of function -pointers-) into structures would
require mechanisms by which data become executable. Some
architectures (e.g., the Harvard architecture) cannot do that.

In architectures that do allow it, you would allow -all- of
your data to be executable (in which case you have all of the
classic stack overflow problems) or else you need a mechanism
to -dynamically- indicate that a particular block of memory is
executable; the existance of such a dynamic method is not at
all certain on any particular system.
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
Dec 31 '05 #2
Walter Roberson wrote:
Albert <al************ *****@gmail.com > wrote:
So structures are useful to group variables, so you can to
refer to a collection as a single entity. Wouldn't it be
useful to also have the ability to collect variable and
functions?

Are there any problems with adding the ability to have
functions encapsulated in structures?


structures are data, functions are code. Having the ability to
store functions (instead of function -pointers-) into structures
would require mechanisms by which data become executable. Some
architectures (e.g., the Harvard architecture) cannot do that.

In architectures that do allow it, you would allow -all- of your
data to be executable (in which case you have all of the classic
stack overflow problems) or else you need a mechanism to
-dynamically- indicate that a particular block of memory is
executable; the existance of such a dynamic method is not at all
certain on any particular system.


Methinks the OP wants lisp.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/>
Dec 31 '05 #3
On 30 Dec 2005 17:12:33 -0800, "Albert"
<al************ *****@gmail.com > wrote in comp.lang.c:
So structures are useful to group variables, so you can to refer to a
collection as a single entity. Wouldn't it be useful to also have the
ability to collect variable and functions?

Ask K&R say, C programs consist of variables to store the input and
functions to manipulate them.

This would make C object-oriented - how cool would that be?

Are there any problems with adding the ability to have functions
encapsulated in structures?


It's already been done. They discuss it in comp.lang.c++, down the
hall. And in comp.lang.java, and I'm sure also in whatever group it
is on Microsoft's server that is devoted to C#.

I know I'll get flamed for this, but with the exception of inheritance
this is really nothing but syntactical sugar. You can write object
oriented programs in C right now.

A perfect example is the FILE data type, declared an <stdio.h>. It
has a creator, fopen(), a destructor, fclose(), and all sorts of
methods you can invoke on it via its pointer, such as fprintf(),
fscanf(), fread(), fwrite(), between its successful creation and its
destruction.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Dec 31 '05 #4
Chuck F. wrote:
Walter Roberson wrote:
Albert <al************ *****@gmail.com > wrote:
So structures are useful to group variables, so you can to
refer to a collection as a single entity. Wouldn't it be
useful to also have the ability to collect variable and
functions?


Are there any problems with adding the ability to have
functions encapsulated in structures?

structures are data, functions are code. Having the ability to
store functions (instead of function -pointers-) into structures
would require mechanisms by which data become executable. Some
architectures (e.g., the Harvard architecture) cannot do that.

In architectures that do allow it, you would allow -all- of your
data to be executable (in which case you have all of the classic
stack overflow problems) or else you need a mechanism to
-dynamically- indicate that a particular block of memory is
executable; the existance of such a dynamic method is not at all
certain on any particular system.

Methinks the OP wants lisp.

Methinks too much credit you offer to the OP.

--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Dec 31 '05 #5
Jack Klein said:
I know I'll get flamed for this,
FLAME ON!!!
but with the exception of inheritance
this is really nothing but syntactical sugar. You can write object
oriented programs in C right now.
Oh. Was that it? Sheesh. Flame off again. (sigh)
A perfect example is the FILE data type, declared an <stdio.h>. It
has a creator, fopen(), a destructor, fclose(), and all sorts of
methods you can invoke on it via its pointer, such as fprintf(),
fscanf(), fread(), fwrite(), between its successful creation and its
destruction.


This is pretty much how I do it, yes. Just a minor nit - FILE isn't (quite)
a perfect example, in at least two ways:

1) FILE should, in my opinion, be a truly opaque type, with the structure
definition hidden internally. typedef struct _iobuf FILE; is already more
than we need to know. The whole schmeer is far, far more than we need to
know.

2) I wish I wish I wish that fclose took FILE ** rather than FILE *, don't
you?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Dec 31 '05 #6
Jack Klein wrote:
.... snip ...
I know I'll get flamed for this, but with the exception of
inheritance this is really nothing but syntactical sugar. You
can write object oriented programs in C right now.

A perfect example is the FILE data type, declared an <stdio.h>.
It has a creator, fopen(), a destructor, fclose(), and all sorts
of methods you can invoke on it via its pointer, such as
fprintf(), fscanf(), fread(), fwrite(), between its successful
creation and its destruction.


I see no reason for any fires. The only problem with your example
is that you can't write (in general) that type in C. As a further
example I suggest my hashlib, which is written in standard C, is
also very much object oriented, offers constructors, destructors,
and methods.

It offers the opaque incomplete object "hshtbl", whose details are
completely hidden. This is unlike FILE*, which has to be
incompletely hidden because of some standards constraints. Of
course hashlib has the advantage of being designed some 30 plus
years later than the FILE system.

See: <http://cbfalconer.home .att.net/download/hashlib.zip>

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/>
Dec 31 '05 #7
Chuck F. said:
Jack Klein wrote:

... snip ...

I know I'll get flamed for this, but with the exception of
inheritance this is really nothing but syntactical sugar. You
can write object oriented programs in C right now.

A perfect example is the FILE data type, declared an <stdio.h>.
It has a creator, fopen(), a destructor, fclose(), and all sorts
of methods you can invoke on it via its pointer, such as
fprintf(), fscanf(), fread(), fwrite(), between its successful
creation and its destruction.


I see no reason for any fires. The only problem with your example
is that you can't write (in general) that type in C.


I must have misunderstood what you mean, because I see no reason whatsoever
why one could not write (in general) that type in C.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Dec 31 '05 #8

"Jack Klein" <ja*******@spam cop.net> wrote
I know I'll get flamed for this, but with the exception of inheritance ^^^^^^^^^^^^^^^ ^^^^^^^^^ this is really nothing but syntactical sugar. You can write object
oriented programs in C right now.

A perfect example is the FILE data type, declared an <stdio.h>. It
has a creator, fopen(), a destructor, fclose(), and all sorts of
methods you can invoke on it via its pointer, such as fprintf(),
fscanf(), fread(), fwrite(), between its successful creation and its
destruction.

Inheritance is crucial.
An object is any set of data items that are "part of the same thing". C
structures are therefore objects. (The C standard further specifies that an
object must be stored contigously in memory. This is a language issue and a
fairly obvious thing to do, but not strictly necessary).

A program becomes "object-oriented" not when it uses objects, but when the
objects enter into relationships with each other. In C++ like most lanauges
that support obect-orientation, this is achieved via inheirtance. However
there are other ways, for example Microsoft Windows objects all respond to
the same message system, Java interfaces specify common methods, text
adventure objects have verb handlers.
Dec 31 '05 #9
Albert a écrit :
So structures are useful to group variables, so you can to refer to a
collection as a single entity. Wouldn't it be useful to also have the
ability to collect variable and functions?

Ask K&R say, C programs consist of variables to store the input and
functions to manipulate them.

This would make C object-oriented - how cool would that be?

Are there any problems with adding the ability to have functions
encapsulated in structures?

You can do this by defining an interface. I do this by using the
following schema:

The object has a pointer to the interface, called "lpVtbl", and other
fields.

The interface is a set of function pointers that defines the methods
available for the object.

For instance:

1) I define a forward declaration to the object
typedef struct _ArrayList ArrayList;

2) I define the interface functions:
typedef struct {
// Returns the number of elements stored
int (*GetCount)(Arr ayList &AL);
// Is this collection read only?
int (*IsReadOnly)(A rrayList &AL);
// Sets this collection read-only or unsets the read-only flag
int (*SetReadOnly)( ArrayList &AL,int flag);
///SNIP /// SNIP /// SNIP
// Pushes a string, using the collection as a stack
int (*Push)(ArrayLi st &AL,void *str);
} ArrayListInterf ace;

3) I define the object
struct _ArrayList {
ArrayListInterf ace *lpVtbl; // The table of functions
size_t count; /* number of elements in the array */
void **contents; /* The contents of the collection */
size_t capacity; /* allocated space in the contents vector */
unsigned int flags; // Read-only or other flags
size_t ElementSize; // Size of the elements stored in this array
};

The usage is now very simple: For instance to get the count of elements
in an array list I do:

ArrayList al;
/// snip initialization, etc

int count = al.lpVtbl->GetCount(&al );

The lcc-win32 compiler uses this feature extensiveley to define
arraylists and other data structures in C.

jacob
Dec 31 '05 #10

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

Similar topics

1
741
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a managed point-of-view I've noticed that: 1) for each managed and unmanaged C function (not C++ classes) I get a public managed static method (defined on a 'Global Functions' class) in the generated assembly with an export name of the form...
5
28675
by: Angel | last post by:
Is there some way to add a C-header file into a C# project? The problem is that this .h file contains several complex structures (over 20) that are used by some unmanaged code. These functions receive these defined structs as parms and modify them during execution. Basically, it'd be something siomple like this (but in C#): #include <parms.h> W32_PARM parm;
11
2583
by: ruffiano | last post by:
A colleague of mine who is a C developer wrote several functions in C which I now need to invoke in my C++ application. This is normally not a problem except that the header file that he wrote contains also several structures and #defines, which I need to use in my C++ application. What is the best way to use the C functions, structures and defines in my code without upsetting the compiler / linker? For the C functions I know I need...
0
9592
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
10230
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10058
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10004
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
7416
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
6678
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
5313
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...
1
3972
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.