Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > A pointer to a function as a part of a struct

Reply
Thread Tools

A pointer to a function as a part of a struct

 
 
Vicent Giner-Bosch
Guest
Posts: n/a
 
      04-02-2010
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.

--
Vicent
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-02-2010
* 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
 
Reply With Quote
 
 
 
 
Vicent Giner-Bosch
Guest
Posts: n/a
 
      04-02-2010

>
> 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.
 
Reply With Quote
 
Vicent Giner-Bosch
Guest
Posts: n/a
 
      04-02-2010

>
> 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...?

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

>
> 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??

--
Vicent
 
Reply With Quote
 
Sousuke
Guest
Posts: n/a
 
      04-02-2010
On Apr 2, 12:06*pm, Vicent Giner-Bosch <(E-Mail Removed)> wrote:
> > 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...?
>
>
>
> > > Is the first time I am trying to do something like this.

>
> > 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??


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
}
};
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      04-02-2010
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
 
Reply With Quote
 
Vicent Giner-Bosch
Guest
Posts: n/a
 
      04-02-2010
> 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.

--
Vicent
 
Reply With Quote
 
Paul N
Guest
Posts: n/a
 
      04-03-2010
On 2 Apr, 13:11, Vicent Giner-Bosch <(E-Mail Removed)> wrote:
> 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++.
 
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
Struct pointer vs. struct array pointer aleksa C Programming 16 02-20-2013 08:20 PM
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
(: Pointer to struct withing pointer to struct :) Zero C Programming 16 11-19-2005 01:27 AM
passing pointer->struct->pointer->struct to function. .. ?? beetle C Programming 2 01-25-2005 06:08 PM
Access from function in struct (pointer to function) to other items in the struct (advanced) Ole C Programming 4 10-26-2004 09:43 PM



Advertisments