Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3405186
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:28:29+00:00 2026-05-18T05:28:29+00:00

I have written the following code: #include stdafx.h #include <iostream> using namespace std; double

  • 0

I have written the following code:

#include "stdafx.h"
#include <iostream>
using namespace std;

double funcA()
{
    return 100.0;
}

int g(double (*pf)())
{
    cout << (*pf)() << endl;
    return 0;
}

int g2(double pf())
{
    cout << pf() << endl;
    return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
    g(&funcA);  // case I
    g(funcA);   // case II

    g2(funcA);  // case III
    g2(&funcA); // case IV
    return 0;
}

I have run the above code on VS2008 and each function call returns ‘100’.
Here is the question:

Q1> Is there any problem in the code?

Q2> It seems that C++ doesn’t make difference between *pf and pf. Is that correct?

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T05:28:29+00:00Added an answer on May 18, 2026 at 5:28 am

    C++ does, in fact, make a distinction between the types double() and double(*)(), but the difference is subtle. When you pass a function type as an argument to a function, the function-type automatically “degrades” to a function pointer. (This is similar, I suppose, to how an array type degrades to a pointer type when passed as a function argument.)

    However, a function type and a function-pointer type are still different types, according to the C++ type-system. Consider the following case:

    void g() { }
    
    template <class F>
    struct Foo
    {
        Foo(const F& f) : func(f)
        { }
    
        void operator()() { func(); }
    
        F func;
    };
    
    
    int main ()
    {
        Foo<void()> f(g);
        f();
    }
    

    This should fail to compile, since you cannot declare a function type as an automatic variable. (Remember, functions are not first-class objects in C++.) So the declaration F func; is invalid. However, if we change our main function to instead instantiate the template using a function pointer, like so:

    int main ()
    {
        typedef void(*function_pointer)();
        Foo<function_pointer> f(g);
        f();
    }
    

    …now it compiles.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written the following code: #include <iostream> using namespace std; template <class T>
I have written something that uses the following includes: #include <math.h> #include <time.h> #include
i have written the following code to copy a string hello world to another
Hii , I have written the following code to improve it for the higher
I'm playing around with Ruby and I have written the following code: module IdAndNameRedefine
I have written the following C99 code and was wondering about the struct declaration.
I have written the following simple test in trying to learn Castle Windsor's Fluent
I have written a KornShell (ksh) script that sets an array the following way:
I have the following in a program (written in VB.NET): Imports Microsoft.Office.Interop.Excel Public Class
(All of the following is to be written in Java) I have to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.