- 全局对象的构造函数会在main函数之前执行。
class CTest
{
public:
CTest() { cout << __FUNCTION__ << endl; }
~CTest() { cout << __FUNCTION__ << endl; }
};
CTest test;
int main()
{
cout << __FUNCTION__<<endl;
return 0;
}
输出为:
CTest::CTest
main
CTest::~CTest
https://blog.youkuaiyun.com/x_iya/article/details/52685139
https://www.cnblogs.com/lgh1992314/p/6616361.html
- 如何查看变量类型
首先引入头文件
然后使用typeid(‘变量名’).name()方法就可以显示出当前变量的数据类型了
#include <typeinfo>
cout << typeid('a' - 0).name() << endl; // int
cout<<typeid(0.0).name()<<endl; // double
cout<<typeid(1000.02).name()<<endl;
cout<<typeid(-1).name()<<endl; // int
cout<<typeid(0).name()<<endl;
cout<<typeid(1).name()<<endl;
cout<<typeid(65530).name()<<endl;
带小数的点的数值默认类型为double,不带小数点的整数默认是int,char, short这些类型,一旦参与运算,全部转换为int类型。
- 各种变量类型的大小
cout << sizeof(char) << " " << sizeof(short) << " * "
<< sizeof(int) << " " << sizeof(long int) << " * "
<< sizeof(long) << " " << sizeof(long long) << " * "
<< sizeof(float) << " " << sizeof(double) << " " << sizeof(long double)
<< endl; // 1 2 * 4 4 * 4 8 * 4 8 8
- 位域
参考资料:https://www.runoob.com/cprogramming/c-data-types.html
- 类型匹配/类型提升
void myfunc(char a)
{
cout<<"char a"<<endl;
}
void myfunc(long b)
{
cout<<"long b"<<endl;
}
int main()
{
short m = 100;
myfunc(m); // compile error
// Call to 'myfunc' is ambiguous
}
此时,编译器不知道匹配哪个函数。
实际上,也不会匹配第二个函数。编译器不知道该匹配谁。C++标准里规定的隐式转换包含了"整形提升(Integral Promotions)"和"整形转换(Integral conversions)"。"整形提升"
是指可将char、unsigned char、signed char、short、unsigned short转换为int或unsigned int。"整形转换"是指可以在任意的两种整数类型之间转换。在重载匹配时,一般的顺序是精确匹配,提升、一般转换。一般的整形转换没有先后顺序,所以编译器不知道匹配谁。
如果将此题加以修改:
void myfunc(char a);
void myfunc(int b);
short m = 100;
myfunc(m) //匹配myfunc(int b)
因为这时提升转换优先级高于一般转换。
- 类型隐式转换
int main()
{
unsigned int a = 1;
long b = 2;
auto c = a + b;
double d = a + b;
char e = a + b;
cout << typeid(c).name() << endl; // unsigned long
cout << typeid(d).name() << endl; // double
cout << typeid(e).name() << endl; // char
cout << typeid((unsigned int)1 / (int)2).name() << endl; // unsigned int
cout << typeid(int(1) + long(2)).name() << endl; // long
cout << typeid((int)1 + (long int)2).name() << endl; // long
cout << typeid((unsigned int)1 + (long)2).name() << endl; // unsigned long
cout << typeid((unsigned int)1 / (long)2).name() << endl; // unsigned long
cout << typeid((long long)1).name() << endl; // __int64
cout << typeid((long long)1 + (unsigned long)2).name() << endl; // __int64
cout << typeid((long long)1 + (unsigned int)2).name() << endl; // __int64
cout << typeid((long long)1 + (unsigned long long)2).name() << endl; // unsigned __int64
cout << typeid((float)1 + (unsigned long long)2).name() << endl; // float
cout << typeid((long double)1 + (double)2).name() << endl; // long double
cout << typeid((float)1 + (double)2).name() << endl; // double
}
long double | long double |
double | double |
float | float |
unsigned long long int | unsigned long long |
long long int | long long |
unsigned long int | unsigned long |
long int | long |
unsigned int | unsigned int |
int | int |
- 规则 1:char、short 和 unsigned short 值自动升级为 int 值。细心的读者可能已经注意到,char、short 和 unsigned short 都未出现在表 1 中,这是因为无论何时在数学表达式中使用这些数据类型的值,它们都将自动升级为 int 类型。
- 规则 2:当运算符使用不同数据类型的两个值时,较低排名的值将被升级为较高排名值的类型。在下面的表达式中,假设 years 是一个 int 变量,而 interestRate 是一个 double 变量。
例外:
cout << typeid((unsigned int)1 + (long)2).name() << endl; // unsigned long
参考资料:http://c.biancheng.net/view/1329.html
- 类型转换运算
int main()
{
float v1 = 1.0;
int v2 = 1;
int v3 = 2;
int v4 = 4;
cout << (v1/v3*v3)<<endl; // 1
cout << (v1/v3*v3)/v3<<endl; // 0.5
cout << static_cast<float>(v1/v3*v3)<<endl; // 1
cout << static_cast<float>(v1/v3*v3)<<endl; // 1
}