Thread: Pointers to functions

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    9

    Pointers to functions

    What are the advantages of having pointers to functions again ... and what kind of set up do they need?

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Pointers to functions allow you to defer deciding what function is called until runtime. Their syntax is generally messy, and virtual functions do the same thing. I'd recommend learning inheritance and polymorphism rather than function pointers.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    9
    Actually inheretence and polymorphism are why I ask. They were both in this "week" and I'm reading the review for the week and this code is what is throwing me off.

    Code:
    class PartsList
    {
    public:
    	PartsList();
    	~PartsList();
    	// needs copy constructor and operator equals
    	Part*	Find(int & position, int PartNumber) const;
    	int		GetCount() const { return itsCount; }
    	Part*	GetFirst() const;
    	void	Insert(Part *);
    	void	Iterate() const;
    	Part*	operator[](int) const;
    private:
    	PartNode * pHead;
    	int itsCount;
    };
    It's mostly the astericks that I'm tripping over ... if you need the function definitions I can edit this and put them on.

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Those aren't function pointers.. just standard pointers. Is it that a function returning a pointer is confusing you?
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    Posts
    187
    SilentStrike is correct, Those function return pointers to obj's.

    Function pointers look like -


    Code:
    void foo(void){     printf("Foo\n");     }
    
    int main(void){
    
        void (*fptr)(void);    // fptr can point to any function that is
    		  // sent (void) and receives (void)
    
        fptr = foo;   // make the function pointer point to foo
       (*fptr)();    // call whatever fptr is pointing to and send ()

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    9
    I guess so ... I didn't even realze that was a function returning a pointer ... it's so much clearer now ... thank you

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. HELP WITH FUNCTIONS and POINTERS!!!
    By cjohnson412 in forum C++ Programming
    Replies: 4
    Last Post: 08-11-2008, 10:48 PM
  3. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  4. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 10:33 PM