New C plus plus notes
进制
十进制:20
八进制:024(以0开头)
十六进制:0x14(以0x或0X开头)
列表初始化
int a = 0;
int b(0);
int c = { 0 };//列表初始化
int d{ 0 }; //列表初始化
特点:使用列表初始化且初始值存在丢失信息的风险,编译器会报错
double a = 10.2;
//int x{ a }; invalid
//int x = { a }; invalid
//int x = { 1.3 };invalid
int x(a); //valid, but d=10
int y = a; //valid, but e=10
extern
单独使用:用于未提供定义的声明
#include <iostream>
extern int a;//未提供定义的声明
int main()
{
int a = 1;
std::cout << a;
}
extern const:在多个文件之间共享const对象
//.h
#pragma once
extern const int a;//valid
//const int b; invalid
const int b = 2;
void print();
//1.cpp
#include <iostream>
#include "MyProject.h"
extern const int a = 9;
int main()
{
print();
}
//2.cpp
#include <iostream>
#include "MyProject.h"
void print()
{
std::cout << a;
std::cout << b;
}
enum
#include <iostream>
enum PlayerStatus
{
PS_Crouched,
PS_Standing,
PS_Walking,
PS_Running
};
int main()
{
PlayerStatus Status;
Status = PS_Crouched;
if(Status==PS_Crouched)
{
std::cout << "PS_Crouched" << std::endl;
}
return 0;
}