我不小心打印了一个没有括号的函数名称,并打印了一个值.我只是好奇这是怎么发生的?无论函数名称或定义如何,输出都是相同的,并且每次运行它都是如此.
编辑:答案清除了我的怀疑,对其他正在阅读它的人 - 明确地将函数名称转换为int工作,即int k =(int)foo;
此测试代码将使事情更清晰:
#include <iostream>
#include <stdio.h>
#include <conio.h>
using namespace std;
void foo(){cout<<'Á';} //FUNCTION IS NEVER CALLED
int main()
{
while(_kbhit()) //JUST TO MAKE SURE BUFFER IS CLEARED
{ getch();} //SAME RESULT WITHOUT THESE TWO STATEMENTS
cout<<foo; //OUTPUT 1
printf("\n%u", foo); //OUTPUT 4199232
/*int k=foo; //CANNOT CONVERT VOID(*)() TO 'INT'*/
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有两个重载函数'func'.func(int,int)在class外定义,func(int)在里面定义.如何从类的成员函数中调用func(int,int)?
#include <iostream>
using namespace std;
int func(int a, int b)
{ return a+b;}
class test
{
int a;
public:
int func(int);
int driver();
};
int test::func(int b)
{ return b;}
int test::driver()
{ return func(10,20);}
int main()
{
test A;
cout<<A.driver(); //ERROR: NO MATCHING FUNCTION TO CALL FUNC(INT,INT)
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我不确定我是否正确地提出了我的问题,所以请随时纠正我.我相信:
在初始化列表中初始化相当于
int a = a;
在构造函数中初始化相当于
int a; a = a;
但我仍然无法弄清楚以下输出的原因:
#include <iostream>
using namespace std;
class test
{
int a,b;
public:
/*
test(int a, int b): a(a), b(b) {} //OUTPUT: 3 2
test(int a, int b) { a = a; b = b;} //OUTPUT: -2 1972965730
test(int c, int d) { a = c; b = d;} //OUTPUT: 3 2
Hence it does work without this pointer. Unless the variable names are same
*/
void print() { …Run Code Online (Sandbox Code Playgroud) c++ ×3