C++基础1

  • 全局对象的构造函数会在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
}

表 1 数据类型排名
long doublelong double
doubledouble
floatfloat
unsigned long long intunsigned long long
long long intlong long
unsigned long intunsigned long
long intlong
unsigned intunsigned int
intint

  • 规则 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
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值