Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > routines in structures ?

Reply
Thread Tools

routines in structures ?

 
 
Skybuck Flying
Guest
Posts: n/a
 
      02-09-2008
Hello,

Is it possible to declare routines in structures ?

Something like:

struct
{
int a, b, c

void Test;

} myStruct;

void myStruct.Test()
{
a = 5;
}

Only a,b,c would be stored in the structure.

This would allow to associate routines with structures a little bit better,
just syntaxtically.

Instead of writing:

myStruct_Test( myStruct );

I could for example write something like:

myStruct.Test();

Bye,
Skybuck.


 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      02-09-2008
In article <d80cc$47ae0e05$541983fa$(E-Mail Removed)1.nb.ho me.nl>,
Skybuck Flying <(E-Mail Removed)> wrote:

>Is it possible to declare routines in structures ?


>Something like:


>struct
>{
> int a, b, c


> void Test;


>} myStruct;


>void myStruct.Test()
>{
> a = 5;
>}


No, this is not legal. What is legal is

struct {
int a, b, c;
void (*Test)(void);
} myStruct;

void myStruct_Test(void) { myStruct.a = 5; }
myStruct.Test = &myStruct_Test;

--
"Ignorance has been our king... he sits unchallenged on the throne of
Man. His dynasty is age-old. His right to rule is now considered
legitimate. Past sages have affirmed it. They did nothing to unseat
him." -- Walter M Miller, Jr
 
Reply With Quote
 
 
 
 
Malcolm McLean
Guest
Posts: n/a
 
      02-09-2008
"Skybuck Flying" <(E-Mail Removed)> wrote in message
> Hello,
>
> Is it possible to declare routines in structures ?
>
> Something like:
>
> struct
> {
> int a, b, c
>
> void Test;
>
> } myStruct;
>
> void myStruct.Test()
> {
> a = 5;
> }
>
> Only a,b,c would be stored in the structure.
>
> This would allow to associate routines with structures a little bit
> better, just syntaxtically.
>
> Instead of writing:
>
> myStruct_Test( myStruct );
>
> I could for example write something like:
>
> myStruct.Test();
>

Probably you want C++.
However use of function pointers can produce something very similar to a C++
struct with function members. However you have to initialise each function
pointer explicitly.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-10-2008
"Skybuck Flying" <(E-Mail Removed)> writes:

> Is it possible to declare routines in structures ?
>
> Something like:
>
> struct
> {
> int a, b, c
> void Test;
> } myStruct;
>
> void myStruct.Test()
> {
> a = 5;
> }


No.

> Instead of writing:
>
> myStruct_Test( myStruct );
>
> I could for example write something like:
>
> myStruct.Test();


<OT>
Interestingly, BCPL (a precursor to B and thus, in some way, C) has
some syntax to assist with virtual method calls. The special kind of
function call:

FUNCTION#(E1, E2, ...)

is taken to mean

E1!0!FUNCTION(E1, E2, ...)

which calls the function pointed to by the element numbered FUNCION in
the zeroth element of the E1 vector (a!x is BCPL version of a[x]).

This syntax was probably added after the split with B but it is
interesting to speculate on how the world would have looked if it had
been there before!
</OT>

--
Ben.
 
Reply With Quote
 
Peter 'Shaggy' Haywood
Guest
Posts: n/a
 
      02-12-2008
Groovy hepcat Skybuck Flying was jivin' in comp.lang.c on Sun, 10 Feb
2008 7:36 am. It's a cool scene! Dig it.

> Is it possible to declare routines in structures ?


If by routines you mean functions, then no. You can, however, declare
pointers to functions in structures. For example:

#include <stdio.h>

struct somestruct
{
void (*somefunc)(void);
};

void thefunc(void)
{
puts("Hello, World!");
}

int main(void)
{
struct somestruct thestruct = {thefunc};
thestruct.somefunc();
return 0;
}

--
Dig the sig!

----------- Peter 'Shaggy' Haywood ------------
Ain't I'm a dawg!!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Routines and algorithms for DRM/SBR Soenke VHDL 0 12-28-2005 10:02 AM
structures, structures and more structures (questions about nestedstructures) Alfonso Morra C Programming 11 09-24-2005 07:42 PM
Type Casting IPv4 and IPv6 structures to Generic Structures tweak C Programming 14 06-11-2004 02:43 PM
Proper serial routines ? Andrew Rich Perl 1 10-24-2003 03:51 PM
How to create global Funtions & Routines Matthew Hood ASP .Net 2 06-25-2003 12:56 PM



Advertisments