? 所有变量必须在使用前进行声明,变量可以在使用前进行声明,不必在函数或过程的开始位置先声明。
? 1. 名称只能使用字母、数字、下划线。
? 2. 名称的第一个字符不能是数字。
? 3. 不能与C++关键字重名。
? 注:以下划线开头的变量名是系统专用的,尽量不要这样命名。长度不要过长,最长尽量不要超过63。
变量含义 | 声明语句 | 变量含义 | 声明语句 |
---|---|---|---|
布尔 | bool var; | 字符/小整数 | char var; |
字符/有符号小整数(和char等价) | signed char var; | 无符号小整数 | unsigned char var; |
短整型 | short var; | 整型 | int var; |
长整型 | long var; | 超长整型 | long long var; |
单精度浮点数 | float var; | 双精度浮点数 | double var; |
枚举类型 | enum <类型名>{<枚举常量表>}; | 共用体 | union Data {int i;float f;}data; |
结构体 | struct 结构体名 {成员列表}; | 类 | class 类名{成员列表}; |
整型数组 | int a[10]; | 引用 | int &a=b; |
指向int的指针 | int* p; | ... | ... |
? 默认初始化是指定义变量时没有指定初值时进行的初始化操作。
? 若变量定义在语句块(即{})外面,那么变量会被默认初始化为0或等同于0的值。
? 若变量定义在语句块里面,变量的值是不确定的。
? 为什么不初始化变量呢?给自己挖坑吗?
? 无论定义于何处都会执行默认构造函数。如果默认构造函数中没有初始化变量,那么变量的值是不确定的。如果不 存在构造函数,编译器会根据需要创建空默认构造函数。
? 值初始化是指用了初始化器但没有提供初始值的情况。变量会被默认初始化为0或等同于0的值。
? 直接初始化就是指使用了初始化器并且提供了初值的情况。
? 对于类来说,直接初始化会调用与实参最佳匹配的构造函数。
? 例如:int a(1);
string str("hello");
Test a(1,2,3);
? 拷贝初始化就是用等号对变量进行赋值的方法,即将右值拷贝给左值。
? 对于内置类型变量来说,直接初始化与拷贝初始化差别几乎可以忽略不计。
? 对于类类型变量来说,直接初始化会调用与实参最佳匹配的构造函数,但是拷贝初始化调用类的拷贝构造函数。
? *注:对类类型变量进行初始化时,如果类的构造函数采用了explicit
修饰并且需要隐式类型转换。此时只能通过 直接初始化而不能通过拷贝初始化。
? 例如:
#include<iostream>
using namespace std;
class Test
{
private:
string str;
public:
explicit Test(int arg): str("number")
{
}
Test(const char* arg): str(arg)
{
}
Test(const Test &arg): str(arg.str) //仅仅为了演示此处不写复制str代码了
{
}
void printStr()
{
cout << str << endl;
}
};
int main()
{
Test t1(97);
t1.printStr();
Test t2 = "asd";
t2.printStr();
//Test t3 = 988;//此处会报错:invalid conversion from 'int' to 'const char*'
//t3.printStr();//此时只能通过直接初始化而不能通过拷贝初始化
Test t4 = t1;
t4.printStr();
return 0;
}
/* 运行结果为:
number
asd
number
--------------------------------
Process exited after 0.1125 seconds with return value 0
请按任意键继续. . .
*/
? 在可以使用直接初始化
和拷贝初始化
的地方都能用列表初始化
。列表初始化对容器的初始化同其他方法更方便一
? 些。
? 特别地,对于容器初始化时有()
和 {}
两种方法。
? 使用()
? 有一个参数时仅构造容器。两个参数时,第一个参数构造容器,第二个初始化容器。
? 使用{}
? 如果参数序列的所有值均与容器类型相同,那么初始化时会将参数列表中的所有值放入容器中,容器的元素数 量和参数数量相同。
? 如果参数序列中的值与容器类型不同,那么会考虑将该值作为构造容器的参数;如果不能作为参数那么就会出 现编译错误。
? 例如:
#include<iostream>
#include <vector>
using namespace std;
template <typename Type>
void testVector(const Type &arg)
{
cout << "大小:" << arg.size() << "个元素:";
for(int i = 0; i < arg.size(); i++)
{
cout << "\'" << arg[i] << "\'" << " ";
}
cout << endl;
}
void testArray(int* arg)
{
cout << "大小:" << 5 << "个元素:";
for(int i = 0; i < 5; i++)
{
cout << "\'" << arg[i] << "\'" << " ";
}
cout << endl;
}
int main()
{
int* arrayP1 = new int[5]();
int array[5] = {1,2,3,4,5};
vector<int> t1;
vector<int> t2(12);
vector<int> t3(12,3);
vector<int> t4{123};
vector<int> t5{12,3};
vector<string> t6;
//vector<string> t7("t7");//此语句错误,无法构造容器
vector<string> t8(8);
vector<string> t9(3,"t9");
vector<string> t10{"t10"};
vector<string> t11{3,"t11"};
testArray(arrayP1);
testArray(array);
testVector(t1);
testVector(t2);
testVector(t3);
testVector(t4);
testVector(t5);
testVector(t6);
//testVector(t7);
testVector(t8);
testVector(t9);
testVector(t10);
testVector(t11);
return 0;
}
/* 运行结果为:
大小:5个元素:'0' '0' '0' '0' '0'
大小:5个元素:'1' '2' '3' '4' '5'
大小:0个元素:
大小:12个元素:'0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0'
大小:12个元素:'3' '3' '3' '3' '3' '3' '3' '3' '3' '3' '3' '3'
大小:1个元素:'123'
大小:2个元素:'12' '3'
大小:0个元素:
大小:8个元素:'' '' '' '' '' '' '' ''
大小:3个元素:'t9' 't9' 't9'
大小:1个元素:'t10'
大小:3个元素:'t11' 't11' 't11'
--------------------------------
Process exited after 0.1704 seconds with return value 0
请按任意键继续. . .
*/
? 动态分配时不加以初始化时,内置数据类型的变量的值是不确定的。类类型变量将调用默认构造函数进行初始化。
? 例如:
? int* a = new int();
? int* a = new int(123);
? vector<int> *t1 = new vector<int> {1,2,3,4};
原文:https://www.cnblogs.com/realZhaZhaHui/p/12538857.html