为什么以下工作?
void foo() {
cout << "Foo to you too!\n";
};
int main() {
void (*p1_foo)() = foo;
void (*p2_foo)() = *foo;
void (*p3_foo)() = &foo;
void (*p4_foo)() = *&foo;
void (*p5_foo)() = &*foo;
void (*p6_foo)() = **foo;
void (*p7_foo)() = **********************foo;
(*p1_foo)();
(*p2_foo)();
(*p3_foo)();
(*p4_foo)();
(*p5_foo)();
(*p6_foo)();
(*p7_foo)();
}
Run Code Online (Sandbox Code Playgroud) 我试图模仿一个构造函数.我试图通过让父EJB具有一个返回指向子结构的指针的函数指针来做到这一点.任何帮助将非常感激.
#include <stdio.h>
#include "stdlib.h"
typedef struct child_t {
char *name;
} Child;
typedef struct parent_t {
Child (*NewChild)();
} Parent;
Child *NewChild() {
Child *child = malloc(sizeof(Child));
child->name = "foo";
return child;
}
int
main()
{
Parent parent;
Child child = parent.NewChild();
}
Run Code Online (Sandbox Code Playgroud) 我正在查看以下代码:
string toUpper(string s) {
string result;
int (*toupperp)(int) = &toupper; // toupper is overloaded
transform(begin(s), end(s), back_inserter(result), toupperp);
return result;
}
Run Code Online (Sandbox Code Playgroud)
我被这条线弄糊涂了:
int (*toupperp)(int) = &toupper; // toupper is overloaded
Run Code Online (Sandbox Code Playgroud)
为什么这条线路必要?
我相信&从内存中检索指向某事物的指针.但是toupper,函数的名称已经是一个指针,不是吗?为什么我们不能这样做:
int (*toupperp)(int) = toupper;
Run Code Online (Sandbox Code Playgroud)
3. int如果函数被重载,为什么它被重载string?
#include<stdio.h>
int main() {
int x;
x=~!printf;
printf("%x",x);
}
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释推导出这个程序输出的过程.