Thread: def args & func pointer

  1. #1
    Banned
    Join Date
    Nov 2007
    Posts
    678

    def args & func pointer

    Code:
    #include <iostream>
    using namespace std;
    
    double p(double b, int e=2)
    {
    	double r = 1.0;
    	while (e--) r *= b;
    	return r;
    }
    
    double (*pp)(double b);
    
    int main()
    {
    	pp = p;
    	cout << p << " " << p(10.0) << " " << pp << " " << pp(10.0) << endl;
    
    	return 0;
    }
    The above code gives me the following error on MSVS .Net 2003:
    test4.cpp(15) : error C2440: '=' : cannot convert from 'double (__cdecl *)(double,int)' to 'double (__cdecl *)(double)'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This makes sense if you understand assembly.
    The function p takes a double and an int (regardless if the int is optional or not), so the function pointer must match that.
    Actually, function pointers do not accept default arguments (not sure about the standard, so anyone feel free to correct that), so
    Code:
    double (*pp)(double, int = 2);
    Wouldn't work either. All arguments must be required.
    Although this might work in VC7.1 (.NET 2003) and lower, it doesn't work anymore in VC8 (2005).
    Last edited by Elysia; 12-28-2007 at 07:43 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Thanks Elysia. You're a programming master! How the hell do you know every thing?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You're not the first who has said that
    That mentioned, it's typically because I have years of experience and you learn lots around this wonderful place!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is a virtual function pointer?
    By ting in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2008, 02:36 AM
  2. scope of a pointer?
    By Syneris in forum C++ Programming
    Replies: 6
    Last Post: 12-29-2005, 09:40 PM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. typedef a func
    By trekker in forum C Programming
    Replies: 4
    Last Post: 07-02-2002, 05:15 AM