ในบทช่วยสอนนี้ เราจะพูดถึงโปรแกรมเพื่อทำความเข้าใจการรบกวนของประเภทใน C++ (ประเภทอัตโนมัติและประเภทการปฏิเสธ)
ในกรณีของคีย์เวิร์ดอัตโนมัติ ประเภทของตัวแปรจะถูกกำหนดจากประเภทของตัวเริ่มต้น นอกจากนี้ ด้วย decltype ยังช่วยให้คุณแยกประเภทของตัวแปรออกจากองค์ประกอบที่เรียกได้
พิมพ์อัตโนมัติ
ตัวอย่าง
#include <bits/stdc++.h>
using namespace std;
int main(){
auto x = 4;
auto y = 3.37;
auto ptr = &x;
cout << typeid(x).name() << endl
<< typeid(y).name() << endl
<< typeid(ptr).name() << endl;
return 0;
} ผลลัพธ์
i d Pi
ประเภทการปฏิเสธ
ตัวอย่าง
#include <bits/stdc++.h>
using namespace std;
int fun1() { return 10; }
char fun2() { return 'g'; }
int main(){
decltype(fun1()) x;
decltype(fun2()) y;
cout << typeid(x).name() << endl;
cout << typeid(y).name() << endl;
return 0;
} ผลลัพธ์
i c