A pointer to a function as a part of a struct

  • Thread starter Vicent Giner-Bosch
  • Start date
V

Vicent Giner-Bosch

Hello.

Say I want to define a struct/type which has several items inside it,
one of them being (a pointer to) a function.

I would do it like this:

typedef struct my_struct {
double (* function_one) (double x) ;
int another_element ;
double another_one ;
} my_struct ;

And when I want to create a new variable/object with that data type, I
would do it like this:

my_struct one_example ;

one_example.function_one = &F ;
one_example.another_element = 3 ;
one_example.another_one = 3.1415 ;


Where F is a previously defined function, something like this:

double F (double x) ;


Is this right?? Is the first time I am trying to do something like
this.

Thank you in advance.
 
A

Alf P. Steinbach

* Vicent Giner-Bosch:
Hello.

Say I want to define a struct/type which has several items inside it,
one of them being (a pointer to) a function.

I would do it like this:

typedef struct my_struct {
double (* function_one) (double x) ;
int another_element ;
double another_one ;
} my_struct ;

And when I want to create a new variable/object with that data type, I
would do it like this:

my_struct one_example ;

one_example.function_one = &F ;
one_example.another_element = 3 ;
one_example.another_one = 3.1415 ;


Where F is a previously defined function, something like this:

double F (double x) ;


Is this right??

Technically it's right.

But the code and idea is all C, not C++.

Ahile technically OK due to C++'s backward compatibility with C, as C++ it's
rather meaningless.

Is the first time I am trying to do something like this.

You haven't told *what* it is you're trying to do.


Cheers & hth.,

- Alf
 
V

Vicent Giner-Bosch

Looks ok to me. But C++ isn't C, so the typedef is unnecessary. Just write:

struct my_struct
{
        // (as before)

};

Cheers,
Stu


@Stuart,

I know, but I would like to keep some backward compatibility with C.
 
V

Vicent Giner-Bosch

Technically it's right.

But the code and idea is all C, not C++.

Ahile technically OK due to C++'s backward compatibility with C, as C++ it's
rather meaningless.


@Alf,

Why is it meaningless in C++? I mean, would you define an object
instead a structure, or...?
You haven't told *what* it is you're trying to do.

I want to do just that: create and manage objects or structs one of
their elements or properties being a function. Say, for example, that
each instance of this struct is a problem to solve, and the element
"function_one" contains the function associated to that problem.

Would it be very different from a C++ approach??
 
S

Sousuke

@Alf,

Why is it meaningless in C++? I mean, would you define an object
instead a structure, or...?





I want to do just that: create and manage objects or structs one of
their elements or properties being a function. Say, for example, that
each instance of this struct is a problem to solve, and the element
"function_one" contains the function associated to that problem.

Would it be very different from a C++ approach??

Your description isn't completely accurate, but you would probably
define an abstract class and then, for each of the "problems", derive
a concrete class:

class Problem
{
public:
virtual ~Problem() {}

virtual void Solve() = 0;
};

class Cancer : public Problem
{
public:
void Solve()
{
// cure cancer
}
};

class Hunger : public Problem
{
public:
void Solve()
{
// cease world hunger :)
}
};
 
K

Kai-Uwe Bux

Vicent Giner-Bosch wrote:

[...]
I want to do just that: create and manage objects or structs one of
their elements or properties being a function. Say, for example, that
each instance of this struct is a problem to solve, and the element
"function_one" contains the function associated to that problem.

Would it be very different from a C++ approach??

Probably. In C++, you would choose from several options. The extremes are
like this:

a) Use a class hierarchy with a virtual function. Here, the observation is
that you usually do not need a function pointer that is modifiable on a per
object basis. Hence, you can encode the function implementation within the
type. E.g.:

struct MathFunction {
virtual
double apply ( double arg ) const = 0;
};

struct Sin : public MathFunction {
virtual
double apply ( double arg ) const {
return ( std::sin( arg ) );
}
};

...


b) If, on the other hand, you do need the flexibility of a modifiable
function within objects of your class, then a function pointer member is
often too restrictive. There are functors in C++, i.e., objects that have
operator() defined and can be called like functions. Those, you could not
assign to a function pointer member. Thus, if you want a member like that,
you would do (using tr1 or C++0X):

struct MyStruct {

std::tr1::function< double ( double ) > function_member;
...

};


c) The option using a function pointer member is somewhat in between and I
have some trouble picturing a use case where it is just the RightThing(tm).
To me, it looks as though it either offers unneeded flexibility or not
enough. But one would need to know much more about the context to provide
solid advice in this regard.


Best

Kai-Uwe Bux
 
V

Vicent Giner-Bosch

Do you have some specific goal in mind, out of interest? The trouble
with writing code in C++ as if it was C is that you end up writing very
non-idiomatic C++. C is a perfectly good language in its own right --
I'm just slightly puzzled as to why you want to write C-like code in C++.

@Stuart,

Actually, what I do is basically writing code in C, but sometimes I
like to use some features that C++ has and C doesn't.

I am not a true C++ programmer (obvious). And I feel more comfortable
with C than with C++, but I asked my question to this forum because I
wanted to know if it was right and if there was a better way to do it
in C++. But I see (again) that C++ means another way of thinking...

Thank you for your answers.
 
P

Paul N

Hello.

Say I want to define a struct/type which has several items inside it,
one of them being (a pointer to) a function.

I would do it like this:

typedef struct my_struct {
    double (* function_one) (double x) ;
    int another_element ;
    double another_one ;

} my_struct ;

And when I want to create a new variable/object with that data type, I
would do it like this:

my_struct one_example ;

one_example.function_one = &F ;
one_example.another_element = 3 ;
one_example.another_one = 3.1415 ;

Where F is a previously defined function, something like this:

double F (double x) ;

Is this right?? Is the first time I am trying to do something like
this.

Just one point to add to what others have said. The "&" in the line

one_example.function_one = &F ;

is optional, you can also write

one_example.function_one = F ;

if you prefer. This is true for both C and C++.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top