English 中文(简体)
当p是功能点时,我们是否应当使用(p)或(p)(......)?
原标题:Should we use p(..) or (*p)(..) when p is a function pointer?
  • 时间:2011-11-22 17:46:24
  •  标签:
  • c++
问题回答

Dereferencing a function pointer yields another function pointer. So, just f(), otherwise you re only obfuscating your code.

Pointers to members are different beasts altogether. They require usage of .* or ->* operators. That s also why you should use std::function (or boost::function) instead of raw pointers to functions/memebers.

Both are okay:

  p();
(*p)();

但是,第一种是可取的,因为它与校长更一致。 例如,你可以撰写一个职能模板:

template<typename Functor>
void f(Functor fun)
{
     fun(); //uniform invocation - it doesn t matter what it is.
}

Now this can be called with function pointers, and functor object, both, which has been made possible only because I have used the first syntax.

故事的道德是:为穿制服,。 书面守则,不论援引是否为职能单位或职能对象,都应当采用相同的方式。

The standard allows all of the forms you use, but unless you want to confuse readers, it s generally best to be explicit: &f to take the address of the function, and (*p)( x, y ) to call it.

You can use either form, but it looks most natural (to me) to use this:

p(x, y);




相关问题
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?

热门标签