pointers and functions

This is a discussion on pointers and functions within the C++ Programming forums, part of the General Programming Boards category; hello! i have this class called _button, it has x,y,w,h and a draw function. Now i need onclick function for ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    8

    pointers and functions

    hello! i have this class called _button, it has x,y,w,h and a draw function. Now i need onclick function for it, i think it sould be pointer type variable, so i can apply something like this is main.cpp: button.onclick=&mycustomfunction. how do i do that and how do i define the variable?

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    you may want to look up polymorphism.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi,
    Now i need onclick function for it, i think it sould be pointer type variable, so i can apply something like this is main.cpp: button.onclick=&mycustomfunction. how do i do that and how do i define the variable?
    Here is an example of using pointers to functions:
    Code:
    #include <iostream>
    using namespace std;
    
    class MyButton
    {
    public:
    	
    	int x;
    	int y;
    	double (*onclick)(int a);
    
    	MyButton()
    	{
    		x = 0;
    		y = 0;
    		onclick = 0;
    	}
    
    	          
    };
    
    double myCustomFunc(int a)
    {
    	cout<<a<<endl;
    	return a + 2.0;
    }
    
    
    int main()
    {
    	MyButton b;
    	b.onclick = myCustomFunc;
    
    	double result = b.onclick(10);
    
    	return 0;
    }
    A pointer to a function must reflect the type of the function, and the type of a function includes the return type and the parameter types. You should note that myCustomFunc is not a member function of MyButton and therefore it does not have access to MyButton member variables unless they are public.
    Last edited by 7stud; 05-08-2005 at 11:16 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 01:56 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 07:53 PM
  5. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 10:33 PM

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