ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
ကားစက်ရုံက ကားတစ်စီး ထုတ်တဲ့အခါ ဘီး မတပ်ရသေးတဲ့ ကားကို ဝယ်သူဆီ မပို့ပါဘူး။ ကား ထွက်လာတဲ့ အခိုက်မှာ အစိတ်အပိုင်း အားလုံး ပြည့်စုံပြီးသား ဖြစ်ရပါတယ်။ Constructor ဆိုတာ ဒီ စစ်ဆေးမှု အဆင့်ပါ — object တစ်ခု create လုပ်တဲ့ အချိန်မှာ C++ က အလိုအလျောက် ခေါ်ပေးတဲ့ special member function ပါ။
Constructor ရဲ့ နာမည်က class နာမည်နဲ့ တူရပြီး return type လုံးဝ မရှိပါဘူး (void တောင် မရေးရပါ)။ Car(std::string brand, int year) လို parameter တွေ ပေးထားရင် Car myCar("Toyota", 2024); လို့ပဲ create လုပ်လို့ ရပြီး brand နဲ့ year မပါတဲ့ Car ကို compiler က လက်မခံတော့ပါဘူး။
Problem → Why → Solution
ပြဿနာ: တစ်ဝက်တစ်ပျက် ဆောက်ထားတဲ့ object
Constructor မရှိရင် object ကို create လုပ်ပြီးမှ field တစ်ခုချင်း ဖြည့်ရပါတယ်။ Field တစ်ခု မေ့သွားတာနဲ့ object က ပြည့်စုံပုံ ပေါ်နေပေမယ့် အထဲမှာ အမှိုက် value ရှိနေပါတယ်။ အောက်က program က compile ဖြစ်ပြီး run ပါတယ်၊ ဒါပေမယ့် year နေရာမှာ run တိုင်း မတူတဲ့ ကျပန်း number ထွက်နိုင်ပါတယ်။
#include <iostream>
#include <string>
class Car {
public:
std::string brand;
int year;
};
int main() {
Car car;
car.brand = "Toyota";
// forgot: car.year = 2024;
std::cout << car.brand << " - " << car.year << '\n'; // garbage year
}ဘာကြောင့် compiler က မတားတာလဲ
Car car; လို့ ရေးတာ တရားဝင်ပါတယ်၊ class ရဲ့ field တွေကို ဘယ်အစဉ်နဲ့ ဘယ်အချိန် ဖြည့်ရမလဲဆိုတာ compiler မသိပါဘူး။ ဖြည့်ဖို့ တာဝန်က object ကို သုံးတဲ့ နေရာတိုင်းမှာ ပြန့်ကျဲနေပါတယ်။ Constructor က အဲဒီ တာဝန်ကို နေရာ တစ်ခုတည်းမှာ စုပေးပါတယ် — parameter တွေ မပါရင် object ကို create လုပ်လို့ကို မရတော့တဲ့အတွက် တစ်ဝက်တစ်ပျက် object ဆိုတာ မဖြစ်နိုင်တော့ပါဘူး။
Core concept: initializer list ပြီးမှ body
Constructor တစ်ခု run တဲ့အခါ အဆင့် နှစ်ဆင့် ရှိပါတယ်။ ပထမအဆင့်မှာ member တိုင်း initialize ဖြစ်ပါတယ် — : brand(b), year(y) လို member initializer list မှာ ရေးထားတဲ့ value ကို သုံးပြီး မရေးထားတဲ့ member တွေအတွက် default member initializer (ရှိရင်) ဒါမှမဟုတ် default construction ကို သုံးပါတယ်။ ဒုတိယအဆင့်မှာမှ { } ထဲက body run ပါတယ်။ Body ထဲမှာ brand = b; လို့ ရေးတာက initialization မဟုတ်ဘဲ empty string အဖြစ် ဆောက်ပြီးသား member ကို ထပ် assign လုပ်တာပါ။
std::string လို member အတွက် body ထဲ assign လုပ်တာက အလုပ် နှစ်ခါ လုပ်တာ (ဆောက်၊ ပြီးမှ အစားထိုး) ဖြစ်ပေမယ့် ရလဒ် မှန်ပါသေးတယ်။ const member၊ reference member နဲ့ default constructor မရှိတဲ့ type ရဲ့ member တွေကိုတော့ initialize ပဲ လုပ်လို့ ရပြီး assign လုပ်လို့ မရလို့ initializer list ကို မဖြစ်မနေ သုံးရပါတယ်။ ဒါကြောင့် initializer list ကို ပုံမှန် နည်းလမ်းအဖြစ် သုံးပြီး body ကို စစ်ဆေးတာ၊ တွက်ချက်တာလို တကယ် လိုအပ်တဲ့ အလုပ်တွေအတွက်ပဲ ချန်ထားပါ။
အရေးကြီးတဲ့ စည်းမျဉ်း တစ်ခုက member တွေ initialize ဖြစ်တဲ့ အစဉ်ပါ။ အဲဒီ အစဉ်က class ထဲမှာ member တွေကို declare လုပ်ထားတဲ့ အစဉ်ကို လိုက်ပြီး initializer list ထဲမှာ ရေးထားတဲ့ အစဉ်ကို မလိုက်ပါဘူး။ Member တစ်ခုရဲ့ initializer က နောက်မှ declare လုပ်ထားတဲ့ member ကို သုံးရင် အဲဒီ member က မ initialize ရသေးလို့ အမှိုက် value ကို ဖတ်မိပါတယ်။ g++ မှာ -Wall ပါရင် ဒီလို ကွာနေတာကို -Wreorder warning နဲ့ ပြပါတယ်၊ initializer list ကို declaration အစဉ်အတိုင်းပဲ အမြဲ ရေးပါ။
Constructor တစ်ခုခုကို ကိုယ်တိုင် ရေးလိုက်တာနဲ့ compiler က parameter မပါတဲ့ default constructor ကို အလိုအလျောက် မထုတ်ပေးတော့ပါဘူး။ Car car; ကို ဆက်ခွင့်ပြုချင်ရင် Car() = default; လို့ ပြန်တောင်းနိုင်ပါတယ်၊ ဒါမှမဟုတ် Car() : Car("Unknown", 2000) {} လို delegating constructor နဲ့ တခြား constructor ဆီ အလုပ်လွှဲလို့ ရပါတယ်။
| Constructor ပုံစံ | ဥပမာ | ဘယ်အချိန် သုံးလဲ |
|---|---|---|
| Default | Car() = default; | Member တိုင်းမှာ default value ရှိတဲ့အခါ |
| Parameterized | Car(std::string b, int y) | Object တည်ဖို့ data လိုအပ်တဲ့အခါ |
| Delegating | Car() : Car("Unknown", 2000) {} | Setup logic ကို ပြန်မရေးချင်တဲ့အခါ |
| explicit single-argument | explicit Car(std::string b) | Implicit conversion ကို တားချင်တဲ့အခါ |
- Argument ကိုက်တဲ့ constructor ကို ရွေး
- Member တွေ declaration အစဉ်အတိုင်း init
- Constructor body run
- Object အသုံးပြုဖို့ အသင့်
Body run တဲ့ အချိန်မှာ member တိုင်း ဆောက်ပြီးသား ဖြစ်နေပါပြီ — အဲဒါကြောင့် initialize လုပ်ချင်ရင် initializer list ထဲမှာ ရေးရပါတယ်။
အထွေထွေ ပုံစံ
class ClassName {
public:
ClassName(TypeA a, TypeB b) : first(a), second(b) {} // initializer list
ClassName() : ClassName(defaultA, defaultB) {} // delegating
explicit ClassName(TypeA a) : first(a) {} // no implicit conversion
ClassName(const ClassName&) = delete; // forbid copying
TypeA first; // initialized 1st: declaration order wins
TypeB second = defaultValue; // default member initializer
};Constructor နာမည်က class နာမည် ဖြစ်ပြီး return type မရှိပါဘူး။ : နောက်က initializer list က member တွေကို တိုက်ရိုက် initialize လုပ်ပါတယ်။ Constructor တစ်ခုက တခြား constructor ကို ခေါ်လို့ ရပြီး single-argument constructor တွေကို explicit လုပ်ထားရင် အလိုလို type ပြောင်းတာ မဖြစ်ပါဘူး။ = delete က compiler ထုတ်ပေးမယ့် function ကို ပိတ်ပါတယ်။
Example: Car constructor
ဒီ example က constructor ရဲ့ အခြေခံ အလုပ်ကို ပြပါတယ် — Car myCar("Toyota", 2024); ဆိုတဲ့ line တစ်ကြောင်းက object ကို create လုပ်ပြီး field နှစ်ခုလုံးကို ဖြည့်ပါတယ်။ Constructor body ထဲမှာ assign လုပ်ထားတာကို သတိထားကြည့်ပါ၊ အောက်က breakdown မှာ အဲဒါကို initializer list နဲ့ ဘယ်လို ရေးမလဲ ပြပါမယ်။
#include <iostream>
#include <string>
using namespace std;
class Car {
public:
string brand;
int year;
Car(string b, int y) {
brand = b;
year = y;
}
void show() {
cout << brand << " - " << year;
}
};
int main() {
Car myCar("Toyota", 2024);
myCar.show();
return 0;
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Line 10
Car(string b, int y) {Class နာမည်နဲ့ တူပြီး return type မပါလို့ constructor ဖြစ်ပါတယ်။ ဒါ တစ်ခုတည်းသာ ရှိတာကြောင့် argument မပါတဲ့ Car c; က အခု compile မဖြစ်တော့ပါဘူး။
- Lines 11-12
brand = b;ဒီ line run တဲ့ အချိန်မှာ brand က empty string အဖြစ် ဆောက်ပြီးသားပါ၊ ဒီမှာ assign ထပ်လုပ်တာပါ။ Car(string b, int y) : brand(b), year(y) {} လို့ ရေးရင် တစ်ခါတည်း initialize လုပ်ပါတယ်။
- Line 21
Car myCar("Toyota", 2024);Object create လုပ်တဲ့ line ပါ။ "Toyota" နဲ့ 2024 က constructor ရဲ့ b နဲ့ y ဆီ ရောက်ပြီး constructor ပြီးတဲ့ အချိန်မှာ myCar က အသုံးပြုဖို့ အသင့်ဖြစ်ပါပြီ။
- Line 16
cout << brand << " - " << year;Constructor က field နှစ်ခုလုံးကို ဖြည့်ပြီးမှ show() ကို ခေါ်နိုင်လို့ အမှိုက် value ထွက်နိုင်ခြေ မရှိပါဘူး။ Output က Toyota - 2024 ပါ။
Toyota - 2024Initializer list မဖြစ်မနေ လိုတဲ့ member များ
အောက်က Car မှာ vin (vehicle identification number) က const ဖြစ်ပါတယ် — ကားတစ်စီးရဲ့ VIN က ဘယ်တော့မှ မပြောင်းသင့်လို့ပါ။ const member ကို body ထဲမှာ assign လုပ်လို့ မရလို့ initializer list ထဲမှာပဲ value ပေးလို့ ရပါတယ်။ mileage ကတော့ initializer list ထဲ မပါပေမယ့် default member initializer ကြောင့် 0 နဲ့ စပါတယ်။
#include <iostream>
#include <string>
class Car {
public:
const std::string vin; // const: can be initialized, never assigned
std::string brand;
int year;
int mileage = 0; // default member initializer
Car(std::string v, std::string b, int y) : vin(v), brand(b), year(y) {}
void show() const {
std::cout << brand << " " << year << " [" << vin << "] "
<< mileage << " km\n";
}
};
int main() {
Car car("JT123", "Toyota", 2024);
car.mileage = 1500;
car.show();
// car.vin = "X999"; would not compile: vin is const
}Program က "Toyota 2024 [JT123] 1500 km" ကို print ထုတ်ပါတယ်။ Initializer list ထဲမှာ vin, brand, year ကို class ထဲက declaration အစဉ်အတိုင်း ရေးထားလို့ -Wreorder warning မတက်ပါဘူး။
Delegating constructor၊ explicit နဲ့ = default
Rectangle မှာ constructor သုံးခု ရှိပါတယ်။ Rectangle() = default; က compiler ထုတ်ပေးတဲ့ default constructor ကို ပြန်တောင်းတာဖြစ်ပြီး width နဲ့ height က default member initializer ကနေ 1 ရပါတယ်။ Rectangle(int side) က logic ကို ပြန်မရေးဘဲ Rectangle(side, side) ဆီ အလုပ်လွှဲပါတယ်။ အဲဒါကို explicit လုပ်ထားလို့ printArea(5) လို ရေးရင် int 5 ကနေ Rectangle ကို အလိုလို မဆောက်ဘဲ compile error ပေးပါတယ်။
#include <iostream>
class Rectangle {
public:
Rectangle() = default; // 1 x 1
Rectangle(int w, int h) : width(w), height(h) {}
explicit Rectangle(int side) : Rectangle(side, side) {} // delegating
int area() const { return width * height; }
int width = 1;
int height = 1;
};
void printArea(const Rectangle& r) { std::cout << "area " << r.area() << '\n'; }
int main() {
Rectangle unit;
Rectangle wide(4, 2);
Rectangle square(3); // runs Rectangle(3, 3) first
printArea(unit);
printArea(wide);
printArea(square);
// printArea(5); error: Rectangle(int) is explicit
}Output က area 1၊ area 8၊ area 9 ပါ။ = delete ကတော့ ဆန့်ကျင်ဘက် ဖြစ်ပါတယ် — compiler ထုတ်ပေးမယ့် function ကို ပိတ်ပစ်ပါတယ်။ Lesson 16 က std::unique_ptr မှာ copy constructor ကို = delete လုပ်ထားလို့ unique_ptr ကို copy ကူးဖို့ ကြိုးစားတာ compile error ဖြစ်ပြီး ownership တစ်ခုတည်းကိုပဲ ထိန်းထားနိုင်တာပါ။
လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Standard library ရဲ့ RAII type တွေက constructor ကို အပြည့်အဝ အသုံးချပါတယ်။ std::ifstream file("data.txt"); က constructor ထဲမှာ file ကို ဖွင့်ပြီး std::lock_guard lock(m); က constructor ထဲမှာ mutex ကို lock လုပ်ပါတယ်။ Object ရှိနေသမျှ resource ကို ကိုင်ထားပြီး destructor က ပြန်လွှတ်ပေးလို့ "ဖွင့်ပြီး ပိတ်ဖို့ မေ့တာ" လို bug တွေ မဖြစ်တော့ပါဘူး။
Application code မှာလည်း class တစ်ခု အလုပ်လုပ်ဖို့ လိုအပ်တဲ့ အရာတွေကို constructor parameter အဖြစ် လက်ခံလေ့ ရှိပါတယ် — OrderService(Database& db, Logger& log) လိုမျိုးပါ။ Dependency တွေကို ဒီလို ထင်ရှားအောင် ပြထားတော့ test ရေးတဲ့အခါ အတု database ကို ထည့်ပေးလို့ ရပါတယ်။ C++ Core Guidelines ကလည်း single-argument constructor တွေကို default အနေနဲ့ explicit လုပ်ဖို့နဲ့ initializer list ကို member declaration အစဉ်အတိုင်း ရေးဖို့ အကြံပြုထားပြီး team အများစုရဲ့ style guide တွေမှာ ဒီ စည်းမျဉ်းတွေ ပါပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. Initializer list အစဉ်ကို ယုံမိတာ
class Range { public: int width; int start; int end; Range(int s, int e) : start(s), end(e), width(end - start) {} }; မှာ width ကို အရင်ဆုံး declare လုပ်ထားလို့ start နဲ့ end မ initialize ရသေးခင် width ကို တွက်ပြီး အမှိုက် ရပါတယ်။ g++ က "'Range::end' will be initialized after [-Wreorder]" နဲ့ "member 'Range::end' is used uninitialized" warning တွေ ပေးပါတယ်။ width ကို နောက်ဆုံး declare လုပ်ပါ ဒါမှမဟုတ် width(e - s) လို့ parameter ကနေ တွက်ပါ။
2. Constructor မှာ return type ရေးမိတာ
void Car(std::string b) { ... } လို့ ရေးရင် g++ က "return type specification for constructor invalid" error ပေးပါတယ်။ Constructor က object ကို ဆောက်တာပဲ ဖြစ်ပြီး value ပြန်မပေးလို့ return type လုံးဝ မရေးရပါဘူး။
3. Default constructor ပျောက်သွားတာ
Car(std::string b, int y) ကို ရေးပြီးတာနဲ့ Car c; ဒါမှမဟုတ် Car garage[3]; က "no matching function for call to 'Car::Car()'" error ပေးပါတယ်။ Default object က အဓိပ္ပာယ် ရှိရင် member တွေကို default value ပေးပြီး Car() = default; ထည့်ပါ၊ အဓိပ္ပာယ် မရှိရင် error က သင့်ကို ကာကွယ်ပေးနေတာပါ။
4. Car c(); လို့ ရေးမိတာ
Car c(); က object မဟုတ်ဘဲ Car ကို return ပြန်တဲ့ function c ကို declare လုပ်တာပါ ("most vexing parse")။ g++ က "empty parentheses were disambiguated as a function declaration [-Wvexing-parse]" လို့ warning ပေးပြီး c.brand ကို သုံးတဲ့ နေရာမှာ error တက်ပါတယ်။ Car c; ဒါမှမဟုတ် Car c{}; လို့ ရေးပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Member တွေကို body ထဲ assign မလုပ်ဘဲ initializer list နဲ့ initialize လုပ်ပြီး declaration အစဉ်အတိုင်း ရေးပါ။
- Constant default value တွေကို int mileage = 0; လို default member initializer နဲ့ ပေးပြီး constructor တိုင်းမှာ ထပ်မရေးပါနဲ့။
- Argument တစ်ခုတည်းနဲ့ ခေါ်လို့ရတဲ့ constructor တိုင်းကို explicit လုပ်ပါ၊ implicit conversion ကို တကယ် လိုချင်မှ ဖြုတ်ပါ။
- Constructor အချင်းချင်း တူညီတဲ့ setup code ရှိရင် delegating constructor နဲ့ နေရာ တစ်ခုတည်းမှာ ထားပါ။
Mini challenge
Mini challenge: Fraction class
Create လုပ်တဲ့ အခိုက်မှာပဲ အမြဲ အရိုးရှင်းဆုံး ပုံစံ (reduced form) ဖြစ်နေတဲ့ Fraction class ကို constructor သုံးမျိုးနဲ့ ရေးပါ။
- numerator ပြီးမှ denominator လို့ declare လုပ်ပြီး Fraction(int n, int d) က initializer list ကို အဲဒီ အစဉ်အတိုင်း သုံးရမယ်
- Constructor body က denominator ကို အပေါင်း ဖြစ်အောင် လုပ်ပြီး std::gcd နဲ့ ချုံ့ရမယ်
- explicit Fraction(int whole) နဲ့ Fraction() တို့က Fraction(int, int) ဆီ delegate လုပ်ရမယ်
- Fraction(6, 8)၊ Fraction(1, -2)၊ Fraction(5)၊ Fraction() တို့ကို print ထုတ်ရမယ်
#include <iostream>
#include <numeric> // std::gcd
class Fraction {
public:
int numerator;
int denominator;
Fraction(int n, int d) /* TODO: initializer list */ {
// TODO: move a negative sign from the denominator to the numerator
// TODO: divide both by their greatest common divisor
}
// TODO: explicit Fraction(int whole) that delegates (whole / 1)
// TODO: Fraction() that delegates (0 / 1)
void print() const {
// TODO: print "numerator/denominator" and a newline
}
};
int main() {
// TODO: build and print 6/8, 1/-2, the whole number 5 and a default Fraction
}ရလဒ်: 3/4 -1/2 5/1 0/1
အရိပ်အမြွက် ကြည့်ရန်
std::gcd က argument တွေရဲ့ absolute value ပေါ်မှာ အလုပ်လုပ်လို့ အနုတ် numerator ကိုလည်း လက်ခံပါတယ်၊ gcd(0, 1) က 1 ပါ။ Delegating constructor ရဲ့ initializer list ထဲမှာ တခြား member ကို ထပ်မထည့်ရပါဘူး — Fraction(int whole) : Fraction(whole, 1) {} လိုမျိုး target constructor ကိုပဲ ရေးရပါတယ်။
အဖြေ ကြည့်ရန်
#include <iostream>
#include <numeric>
class Fraction {
public:
int numerator;
int denominator;
Fraction(int n, int d) : numerator(n), denominator(d) {
if (denominator < 0) {
numerator = -numerator;
denominator = -denominator;
}
int g = std::gcd(numerator, denominator);
if (g != 0) {
numerator /= g;
denominator /= g;
}
}
explicit Fraction(int whole) : Fraction(whole, 1) {}
Fraction() : Fraction(0, 1) {}
void print() const { std::cout << numerator << "/" << denominator << '\n'; }
};
int main() {
Fraction(6, 8).print();
Fraction(1, -2).print();
Fraction(5).print();
Fraction zero;
zero.print();
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Constructor က object create လုပ်တဲ့ အခိုက်မှာ အလိုအလျောက် run ပြီး class နာမည်နဲ့ တူကာ return type မရှိပါဘူး။
- Member initializer list က member တွေကို initialize လုပ်ပြီး body ကတော့ အဲဒီနောက်မှ run ပါတယ် — const နဲ့ reference member တွေအတွက် list က မဖြစ်မနေ လိုပါတယ်။
- Member တွေ initialize ဖြစ်တဲ့ အစဉ်က declaration အစဉ်ဖြစ်လို့ list ကို အဲဒီ အစဉ်အတိုင်း ရေးပါ (-Wreorder)။
- Delegating constructor က logic ကို ပြန်သုံးပေးပြီး explicit က implicit conversion ကို တားကာ = default / = delete က compiler ထုတ်ပေးတဲ့ function တွေကို ထိန်းချုပ်ပါတယ်။
The Current ISO C++ Standard (C++23) — Standard C++ Foundation