Function reference

This is a discussion on Function reference within the C++ Programming forums, part of the General Programming Boards category; Can I create a reference to a function, as with a variable? If not, can I do the same thing ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    367

    Function reference

    Can I create a reference to a function, as with a variable?

    If not, can I do the same thing in another way?

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Do you mean a function pointer?
    Code:
    #include <stdio.h>
    
    typedef void (*vpfn) (void);
    
    void sayHi (void);
    void sayBye (void);
    
    int main (void) {
    	vpfn myFn = sayHi; // NOT myFn = sayHi();
    	myFn();
    	printf ("And...\n");
    	myFn = sayBye;
    	myFn();
    	
    	return 0;
    }
    
    void sayHi (void) {
    	printf ("Hi!\n");
    	return;
    }
    
    void sayBye (void) {
    	printf ("Bye!\n");
    	return;
    }
    Code:
    :!a.out                                                       
    Hi!
    And...
    Bye!
    Or if you mean something else, could you describe a bit more what you want to do?
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Textbox
    By maxorator in forum Windows Programming
    Replies: 20
    Last Post: 09-25-2005, 10:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21