Hi all,
Let's say I have the following code:
typedef struct
{
void (*function)();
} TYPE;
void function()
{
printf("The caller structure is...?\n");
}
int main()
{
TYPE x;
TYPE y;
x->function = *function;
y->function = *function;
x.function();
y.function();
}
Is it possible to know where am I calling the function "function" from?
(if it is from the structure x or y?) I don't want to pass x or y as a
argument.
Thank you in advance,
André 6 23862
On Jan 2, 2:47 pm, "André" <andre....@gmail.comwrote:
Hi all,
Let's say I have the following code:
typedef struct
{
void (*function)();
} TYPE;
void function()
{
printf("The caller structure is...?\n");
}
int main()
{
TYPE x;
TYPE y;
x->function = *function;
y->function = *function;
Here you probably meant "x.function = function;".
>
x.function();
y.function();
}
Is it possible to know where am I calling the function "function" from?
(if it is from the structure x or y?) I don't want to pass x or y as a
argument.
Thank you in advance,
André
The function is not being called from the structure, the structure only
contains a pointer to it, so the answer would be no.
--
WYCIWYG - what you C is what you get.
André wrote:
Hi all,
Let's say I have the following code:
typedef struct
{
void (*function)();
} TYPE;
TYPE NAMES USUALLY DON'T SHOUT AT YOU. IT'S MACROS
THAT SHOUT.
void function()
{
printf("The caller structure is...?\n");
}
int main()
{
TYPE x;
TYPE y;
x->function = *function;
y->function = *function;
Surely you mean
x.function = function;
y.function = function;
since `x` and `y` are struct objects, not pointers-to-struct,
and the `*` applied to `function` is here pointless. (The
name `function` refers to the function object, which decays into
a function pointer. The `*` dereferences it back to the function
object, which then immediately decays back into the pointer before
you assign it to the function fields.)
x.function();
y.function();
}
Is it possible to know where am I calling the function "function" from?
(if it is from the structure x or y?) I don't want to pass x or y as a
argument.
Tough. You'll have to. Why don't you want to?
--
Chris "hopefully not Pyecroft" Dollin
Meaning precedes definition.
Surely you mean
>
x.function = function;
y.function = function;
since `x` and `y` are struct objects, not pointers-to-struct,
and the `*` applied to `function` is here pointless. (The
name `function` refers to the function object, which decays into
a function pointer. The `*` dereferences it back to the function
object, which then immediately decays back into the pointer before
you assign it to the function fields.)
Ah, ok.
Tough. You'll have to. Why don't you want to?
I wanted to build a OO-like structure, so that x and y would be objects
and "function" would be a methods. I know I can do something like
function(type* object), but I meant to do things differently.
I was trying to think of a possible hack, but since I wanted to be ANSI
portable, it's kinda tough... Thank you anyway.
André
André wrote:
>Surely you mean
x.function = function; y.function = function;
since `x` and `y` are struct objects, not pointers-to-struct, and the `*` applied to `function` is here pointless. (The name `function` refers to the function object, which decays into a function pointer. The `*` dereferences it back to the function object, which then immediately decays back into the pointer before you assign it to the function fields.)
Ah, ok.
>Tough. You'll have to. Why don't you want to?
I wanted to build a OO-like structure, so that x and y would be objects
and "function" would be a methods. I know I can do something like
function(type* object), but I meant to do things differently.
There's no need to: a typical implementation strategy for
OO-languages is for a method call `x.f(y)` to call the method
`f` (which may depend on `x`, eg by being a field of `x`, or
being a field of a type object pointed to by `x`) with the
arguments `x` and `y`. So your `something like function(type* object)`
is just a C implementation of that tactic.
It may not look as /pretty/, but that's another matter.
(It may be an important matter, since persistent ugliness can
be a bar to maintenance. But that's a different type of technical.)
--
Chris "hopefully not Pyecroft" Dollin
"The path to the web becomes deeper and wider" - October Project
I wanted to build a OO-like structure, so that x and y would be objects
and "function" would be a methods. I know I can do something like
function(type* object), but I meant to do things differently.
There's no need to: a typical implementation strategy for
OO-languages is for a method call `x.f(y)` to call the method
`f` (which may depend on `x`, eg by being a field of `x`, or
being a field of a type object pointed to by `x`) with the
arguments `x` and `y`. So your `something like function(type* object)`
is just a C implementation of that tactic.
I see. My point was exactly a attempt to be able to call 'x.f(y)' from
within C. Too bad there isn't a simpler way to do it. I'm thinking that
maybe a object table may help on this matter. We'll see.
André
"André" <an*******@gmail.comwrote in message
news:11**********************@n51g2000cwc.googlegr oups.com...
I wanted to build a OO-like structure, so that x and y would be
objects
and "function" would be a methods. I know I can do something like
function(type* object), but I meant to do things differently.
If you want that syntax, C++ is down the hall. If you're going to stick
to C, then you'll need to add an object parameter so that the function
can know the context it's being called in. In fact, this is what many
(most?) C++ implementations do under the hood; there's a hidden first
parameter called "this" which the compiler automatically inserts.
Hiding it is just a convenience to the programmer; C doesn't do that, so
you'll need to add a "this" parameter yourself.
S
--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking
--
Posted via a free Usenet account from http://www.teranews.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: wired |
last post by:
Hi,
I've just taught myself C++, so I haven't learnt much about style or
the like from any single source, and I'm quite styleless as a result.
But at the same time, I really want nice code and I...
|
by: Robert Street |
last post by:
Hi!
I'm rather new at c++ and I'm totally confused with this kind of
typecasting:
typedef signed char int8_t;
typedef signed short int16_t;
typedef struct broadcast_hdr
{
|
by: beetle |
last post by:
Hello,
I'm storing data in several different binary tree's.
The root node is located in a struct containing general data about the
tree.
struct lnode {
char *fname;
int nentry;
|
by: Morgan Cheng |
last post by:
Hi All,
I was taught that argument valuse is not supposed to be changed in
function body. Say, below code is not good.
void foo1(int x)
{
x ++;
printf("x+1 = %d\n", x);
}
It should be...
|
by: gmtonyhoyt |
last post by:
It's been mentioned to me that, in standard c, that you can have
structures behave very much like classes in relation to something to do
with function calls within a structure.
Now, I didn't get...
|
by: goldfita |
last post by:
I love this place. This one problem keeps coming back to bite me.
Suppose I have the following situation (actually I do).
func1()
{
codeA
somefunc1()
codeB
}
|
by: gcary |
last post by:
I am having trouble figuring out how to declare a pointer to an array
of structures and initializing the pointer with a value. I've looked
at older posts in this group, and tried a solution that...
|
by: bowlderster |
last post by:
Hello,all.
I want to get the array size in a function, and the array is an argument of the function.
I try the following code.
/***************************************
*/
#include<stdio.h>...
|
by: etienne |
last post by:
Hello,
I am looking for help for a cast problem:
I want to run functions in thread, using the pthread_create function.
The function to run is the first argument of pthread_create and is
expected...
|
by: =?Utf-8?B?dmxhZGltaXI=?= |
last post by:
Hi, i have big subsystem written in old C and published by dll (and lib). Dll
functions do:
1) allocate global memory for internal structures
2) controls dll subsystem (communicate by sockets,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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...
|
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...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |