A pointer to a function as a part of a struct

Discussion in 'C++' started by Vicent Giner-Bosch, Apr 2, 2010.

  1. 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
     
    Vicent Giner-Bosch, Apr 2, 2010
    #1
    1. Advertising

  2. * 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
     
    Alf P. Steinbach, Apr 2, 2010
    #2
    1. Advertising


  3. >
    > 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.
     
    Vicent Giner-Bosch, Apr 2, 2010
    #3

  4. >
    > 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
     
    Vicent Giner-Bosch, Apr 2, 2010
    #4
  5. Vicent Giner-Bosch

    Sousuke Guest

    On Apr 2, 12:06 pm, Vicent Giner-Bosch <> 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 :)
    }
    };
     
    Sousuke, Apr 2, 2010
    #5
  6. Vicent Giner-Bosch

    Kai-Uwe Bux Guest

    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
     
    Kai-Uwe Bux, Apr 2, 2010
    #6
  7. > 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
     
    Vicent Giner-Bosch, Apr 2, 2010
    #7
  8. Vicent Giner-Bosch

    Paul N Guest

    On 2 Apr, 13:11, Vicent Giner-Bosch <> 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++.
     
    Paul N, Apr 3, 2010
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Fogelklou
    Replies:
    36
    Views:
    1,391
    Chris Fogelklou
    Apr 20, 2004
  2. Ole
    Replies:
    4
    Views:
    603
    Michael Wojcik
    Oct 26, 2004
  3. beetle
    Replies:
    2
    Views:
    913
    beetle
    Jan 25, 2005
  4. Zero
    Replies:
    16
    Views:
    660
    Barry Schwarz
    Nov 19, 2005
  5. aleksa

    Struct pointer vs. struct array pointer

    aleksa, Feb 20, 2013, in forum: C Programming
    Replies:
    16
    Views:
    471
    Shao Miller
    Feb 20, 2013
Loading...

Share This Page