คุณสามารถ initialize ตัวแปรได้โดยใช้ assignment operator หรือใช้ constructor เมื่อ initialize ตัวอย่าง -
int i = 0; MyClass instance(1, "Hello");
มันจะถูก initialize อัตโนมัติหาก -
- เป็น instance ของ class/struct ที่ default constructor initialize primitive types ทั้งหมด เช่น
MyClass instance; - คุณใช้ array initializer syntax เช่น
int a[10] = {}(ทั้งหมดเป็นศูนย์) หรือint a[10] = {1,2};(ศูนย์ทั้งหมดยกเว้น 2 ตัวแรก: a[0]==1 และ a[1]==2) - เป็นตัวแปร global/extern
- ถูกกำหนดเป็น static