English 中文(简体)
职能与职能单位之间的区别
原标题:Difference between Functions and Function Pointers as Arguments

这是一个非常简单的问题,使我感到奇怪。 考虑以下法典:

#include <iostream>

int three()
{
    return 3;
}

void foo(int func(void))
{
    std::cout << func() << std::endl;
}

void bar(int (*func)(void))
{
    std::cout << func() << std::endl;
}

int main()
{
    foo(three);
    bar(three);
    return 0;
}

// output:
// 3
// 3

你可以看到,我们有两个职能,作为唯一的理由,承担另一个职能。 然而,其功能原型各不相同。 基本上,我们有<条码> 避免 foo(int func(ave))和<条码> 避免条码(int (*func)(ave)。 首先,它像<代码>foo<>/code>正在发挥这一功能,<编码>bar<>/code>正在担任一项职务。

However, they both produce the exact same results, and have the exact same body, and are called in exactly the same manner.

My question is, is there an actually hidden difference between foo and bar? Is this simply an optional syntax in C++? Is one of the two cases considered "bad style" in C++?

如果我的编辑是一个促成因素,则使用“视力”演播室的Im。

最佳回答

There is no difference: the types of foo and bar are the same: int(int(*)()).

不存在职能类型的参数:如果参数出现在功能类别辛醇中(如<代码>int func(避免)),则该参数转化为相应的点对功能类型(如<编码>int(*func)(避免))。

这一转变与以阵列合成物宣布的参数(例如int a[])如何转化为相应的代号(int* a)。

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签