Thread: Function Pointers in Structs and Classes?

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    24

    Function Pointers in Structs and Classes?

    Is it possible to have function pointers in structs and classes? Here is the structure of the code I am trying to write:

    Code:
    struct function_pointer
    {
            int id;
            int *func_1(int,...);
    };
    
    int func_2(int argument,...);
    {
        va_list arguments;
        va_start(arguments,argument);
        for (int i=0;i<argument;i++)
        {
              cout << va_arg(arguments,int);
        }
        va_end(arguments);
    }
    
    int main()
    {
        function_pointer x;
        x.id = 10;
        func_2(2,4,5);
        x.func_1 = &func_2; //error line
        x.func_1(2,2,5);
        cin.get();
    }

    There is this error: invalid use of member (did you forget the `&' ?)
    I've never seen it before now. Suggestions?
    Last edited by Ken Fitlike; 09-07-2006 at 12:47 PM. Reason: code tags added

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    First, you have a semicolon after your arguements in func_2 that has to go. Second, you don't have a function pointer in that struct you have a function that returns a pointer to an int. Why you have it returning an int, I don't know, you aren't returning anything in the function, anyway.

    Code:
    // This is a function pointer
    void (*func_1)(int,...);
    };
    
    void func_2(int argument,...)
    {
    Sent from my iPadŽ

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > int *func_1(int,...);
    Would be
    int (*func_1)(int,...);

    > x.func_1 = &func_2;
    x.func_1 = func_2;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    24
    Thanks very much! That was just a dumbed-down example of what I had. Ready for the next one?

    Can I have a function like so:

    Code:
    void arg_func(...)
    Where I use the va_ functions to dissect the argument list? The problem I am having is that va_start takes two arguments, and the second one must be the name of the first variable... I don't have one! Is there another choice of function?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Where I use the va_ functions to dissect the argument list?
    You can't, va_args needs at least one fixed argument.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    24
    And there is no option, other than va_arg?

    On an entirely different note, can I create argument lists as variables and then pass them to a function, ie:

    Code:
    void function(int a, int b){define function};
    _arglisttype arg_list x;
    // Add two ints to x
    function(x);
    Or, even:

    Code:
    void function(_arglisttype){define function};
    _arglisttype arg_list x;
    // Add two ints to x
    function(x);
    Last edited by LPP; 09-07-2006 at 01:32 PM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Once you have an arglist, you can write your own functions like vsprintf()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Class function pointers are much different than C function pointers. I recommend googling it which will give you a host of good sites to browse on the topic.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structs or classes?
    By legit in forum C++ Programming
    Replies: 4
    Last Post: 06-28-2009, 10:16 AM
  2. classes, structs and unions
    By Luciferek in forum C++ Programming
    Replies: 24
    Last Post: 08-09-2008, 10:26 AM
  3. Why use Classes instead of Structs?
    By yaya in forum C++ Programming
    Replies: 12
    Last Post: 03-16-2008, 12:39 AM
  4. Are structs and classes the same thing?
    By dwks in forum C++ Programming
    Replies: 6
    Last Post: 11-25-2005, 03:21 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM