ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Reference ဆိုတာ ရှိပြီးသား variable တစ်ခုရဲ့ ဒုတိယ နာမည်ပါ။ သူငယ်ချင်း အောင်ကို အိမ်မှာ "ကိုကို" လို့လည်း ခေါ်ရင် အဲဒါ လူတစ်ယောက်တည်းအတွက် နာမည် နှစ်ခုပါ — ကိုကို့အပေါ် ဖြစ်သမျှ အောင့်အပေါ် ဖြစ်ပါတယ်။ C++ မှာ std::string& alias = course; က alias ကို course ရဲ့ နောက်ထပ် နာမည်တစ်ခု ဖြစ်စေပြီး ဘာကိုမှ copy မကူးပါဘူး။
Reference တွေ အရေးအကြီးဆုံးက function parameter တွေမှာပါ။ String ဒါမှမဟုတ် vector အကြီးကို value နဲ့ ပို့ရင် copy ကူးပါတယ်၊ reference နဲ့ ပို့ရင်တော့ caller ရဲ့ object ကိုယ်တိုင်ကို function လက်ထဲ ထည့်ပေးတာပါ။ const std::string& လို const ထပ်ထည့်လိုက်ရင် function က original ကို copy မကူးဘဲ ဖတ်နိုင်ပြီး ပြောင်းခွင့်တော့ မရှိပါဘူး။
Problem → Why → Solution
ပြဿနာ: copy ပေါ်မှာပဲ ဖြစ်သွားတဲ့ ပြောင်းလဲမှုတွေ
စာရင်းထဲက ဈေးနှုန်းတိုင်းကို နှစ်ဆ လုပ်ချင်တယ် ဆိုပါစို့။ အောက်က loop က compile ဖြစ်တယ်၊ run လည်း ရတယ်၊ ဒါပေမယ့် ဘာမှ မပြောင်းပါဘူး — iteration တိုင်းမှာ element တစ်ခုကို price ထဲ copy ကူးပြီး copy ကို နှစ်ဆလုပ်ကာ လွှင့်ပစ်လိုက်လို့ပါ။ Function တွေမှာလည်း ဒီထောင်ချောက် အတူတူပါ — std::vector<int> လို့ ကြေညာထားတဲ့ parameter က ကိုယ်ပိုင် copy ကို ရလို့ function ထဲက ပြင်ဆင်မှုတွေ return ပြန်တာနဲ့ ပျောက်သွားပြီး container အကြီးဆိုရင် အဲဒီ copy က call တိုင်းမှာ အချိန်နဲ့ memory ကိုပါ ကုန်စေပါတယ်။
#include <iostream>
#include <vector>
void addDiscount(std::vector<int> prices) { // receives a copy
for (int& price : prices) price -= 10;
}
int main() {
std::vector<int> prices{100, 250, 40};
for (int price : prices) price *= 2; // doubles a copy
addDiscount(prices); // edits another copy
std::cout << prices[0] << '\n'; // still 100
}C++ က ဘာကြောင့် default အနေနဲ့ copy ကူးတာလဲ
C++ မှာ int ဒါမှမဟုတ် std::vector<int> type variable တစ်ခုက value တစ်ခုပါ — တစ်ခုကနေ နောက်တစ်ခုကို initialize လုပ်ရင် သီးခြား copy တစ်ခု ဖြစ်လာပါတယ်။ ဒါက ဘေးကင်းပြီး ကြိုမှန်းလို့ရပေမယ့် reference ကို ကိုယ်တိုင် မတောင်းမချင်း ပြင်ဆင်မှုတွေ original ဆီ ဘယ်တော့မှ မရောက်ဘူးလို့ ဆိုလိုပါတယ်။ GCC က loop bug ကို variable 'price' set but not used လို့ သဲလွန်စ ပေးပါသေးတယ်။ int& price နဲ့ std::vector<int>& prices လို့ ရေးလိုက်ရင် နာမည် နှစ်ခုလုံးက caller ရဲ့ object တွေကို ရည်ညွှန်းပါတယ်။
Core concept: object တစ်ခု၊ နာမည် နှစ်ခု
Reference ကို type နောက်မှာ & ထည့်ပြီး ကြေညာပါတယ် — int& r = x; က r ကို x ရဲ့ နောက်ထပ် နာမည် ဖြစ်စေပါတယ်။ Pointer လို address သိမ်းထားတဲ့ သီးခြား object မဟုတ်ဘဲ alias တစ်ခုပါ၊ r ကို သုံးတိုင်း x ကို သုံးတာပါပဲ။ ဒါကြောင့် reference ကို ကြေညာတဲ့အချိန်မှာ initialize လုပ်ရပါမယ် (int& r; ဆိုရင် 'r' declared as reference but not initialized ဆိုတဲ့ error တက်ပါတယ်)၊ နောက်ပိုင်းမှာ တခြား variable ဆီ ဘယ်တော့မှ re-bind လုပ်လို့ မရပါဘူး။
std::string& alias = course;
----------------------------
course ---+
+---> [ "Basic C++" ] one object, two names
alias ---+
alias = other; copies the text of other INTO course;
alias still names courseအဲဒီ နောက်ဆုံး စည်းမျဉ်းက လူတွေကို အံ့ဩစေပါတယ်။ int& r = x; ပြီးနောက် r = y; လို့ ရေးတာက r ကို y ဆီ ညွှန်စေတာ မဟုတ်ပါဘူး — r က x ဖြစ်နေလို့ y ရဲ့ value ကို x ထဲ copy ကူးတာပါ။ မှန်ကန်တဲ့ program မှာ reference က null လည်း မဖြစ်နိုင်ပါဘူး၊ ဒါကြောင့် value က အမြဲ ရှိကိုရှိရမယ့်နေရာမှာ reference က သဘာဝကျတဲ့ ရွေးချယ်မှုဖြစ်ပြီး "ဘာမှ မရှိဘူး" ကလည်း တရားဝင် အဖြေ ဖြစ်နိုင်ရင်တော့ pointer က မှန်ကန်တဲ့ tool ပါ။
Reference တွေ တကယ် အသုံးဝင်တာက parameter တွေမှာပါ၊ အသုံးများတဲ့ ပုံစံ သုံးမျိုး ရှိပါတယ်။ f(std::string s) လို pass by value က function ကို ကိုယ်ပိုင် copy ပေးပါတယ် — int၊ double လို type အသေးလေးတွေ ဒါမှမဟုတ် function က copy လိုအပ်နေတာ ဖြစ်ရင် မှန်ပါတယ်။ f(std::string& s) လို pass by reference က function ကို caller ရဲ့ object ကို ပြောင်းခွင့် ပေးပါတယ်။ f(const std::string& s) လို pass by const reference ကတော့ object အကြီးကို copy မကူးဘဲ ဖတ်ခွင့်ပေးပြီး ပြောင်းဖို့ ကြိုးစားတာမှန်သမျှကို compiler က ငြင်းပါတယ်။ Non-const reference က temporary တွေကိုလည်း လက်မခံပါဘူး — shout က std::string& ယူထားရင် shout(std::string("hi")) က compile မဖြစ်ပါဘူး၊ temporary ကို ပြောင်းတာ အဓိပ္ပာယ် မရှိလို့ပါ။
| Parameter | Copy ကူးလား | Caller ရဲ့ object ကို ပြောင်းနိုင်လား |
|---|---|---|
| T value | ကူးတယ် | မပြောင်းနိုင် |
| T& value | မကူးဘူး | ပြောင်းနိုင် |
| const T& value | မကူးဘူး | မပြောင်းနိုင် |
Reference က သူ ခေါ်တဲ့ object ရဲ့ သက်တမ်းကို ဘယ်တော့မှ မဆွဲဆန့်ပေးပါဘူး (const reference နဲ့ ချိတ်ထားတဲ့ temporary အတွက် အထူး စည်းမျဉ်း တစ်ခုကလွဲရင်)။ Object က အရင် သေသွားရင် reference က pointer လိုပဲ dangling ဖြစ်ပါတယ်။
- T& r = x; က x ပေါ် နာမည်ကတ် ထပ်ကပ်
- r ကို သုံးတိုင်း x ကို သုံးတာ
- r = y က y ကို x ထဲ copy၊ re-bind မဟုတ်
- x သေရင် r က dangle ဖြစ်
Object အကြီးကို ဖတ်ဖို့ const T&၊ ပြောင်းဖို့ T&၊ value အသေးလေးတွေအတွက် ရိုးရိုး T ကို သုံးပါ။
အထွေထွေ ပုံစံ
Type& alias = existing; // must be initialised, cannot be re-bound
void read(const Type& value); // no copy, read-only
void change(Type& value); // no copy, may modify the caller's object
void own(Type value); // gets its own copy
for (Type& item : container) { /* modify each element */ }
for (const Type& item : container) { /* read without copying */ }& က type ရဲ့ နောက်မှာ ရှိပါတယ်။ Parameter list ထဲမှာ function က caller ရဲ့ object ကို မြင်မလား၊ copy ကို မြင်မလားကို & က ဆုံးဖြတ်ပြီး ပြောင်းခွင့် ရှိမရှိကို const က ဆုံးဖြတ်ပါတယ်။ Range-based for ရဲ့ loop variable မှာလည်း ဒီရွေးချယ်မှု အတူတူပါပဲ။
Example: reference တွေကနေ course နာမည် ပြောင်းခြင်း
ဒီ program က course ကို ဒုတိယ နာမည်တစ်ခု ပေးပြီး const reference နဲ့ print ထုတ်ပါတယ်၊ non-const reference parameter နဲ့ နာမည်ပြောင်းပါတယ်၊ နောက်ဆုံးမှာ reference ကို assign လုပ်တာက reference ကို re-bind လုပ်တာ မဟုတ်ဘဲ value ကို copy ကူးတာ ဖြစ်ကြောင်း ပြပါတယ်။
#include <iostream>
#include <string>
void renameCourse(std::string& courseName) {
courseName = "Advanced C++";
}
void printCourse(const std::string& courseName) {
std::cout << "Course: " << courseName << '\n';
}
int main() {
std::string course = "Basic C++";
std::string& alias = course;
printCourse(alias);
renameCourse(course);
printCourse(course);
std::string other = "Rust";
alias = other;
other = "Go";
std::cout << "course: " << course << ", other: " << other << '\n';
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Lines 4-6
void renameCourse(std::string& courseName) {courseName က reference parameter ဖြစ်လို့ function ထဲမှာ assign လုပ်တာက caller ရဲ့ string ကို ပြောင်းပါတယ်။ ဝင်လာတဲ့အခါ string ကို copy မကူးပါဘူး။
- Lines 8-10
void printCourse(const std::string& courseName) {ဖတ်ရုံပဲ ဖတ်မယ့် string ကို လက်ခံဖို့ ပုံမှန်နည်းက const std::string& ပါ — copy မရှိသလို ပြောင်းဖို့ ကြိုးစားရင်လည်း compiler က ငြင်းပါတယ်။
- Line 14
std::string& alias = course;alias ကို course ကို ခေါ်တဲ့ နာမည်အဖြစ် initialize လုပ်ပါတယ်။ Memory ထဲမှာ string တစ်ခုတည်းပဲ ရှိနေဆဲပါ။
- Line 17
renameCourse(course);Function က course ကိုယ်တိုင်ကို ရလို့ နောက်က printCourse() က Advanced C++ ကို ပြပါတယ်။
- Lines 20-23
alias = other;ဒါက alias ကို other ဆီ re-bind လုပ်တာ မဟုတ်ပါဘူး — alias က course ဖြစ်နေလို့ "Rust" ကို course ထဲ copy ကူးတာပါ။ နောက်မှ other က "Go" ဖြစ်သွားပေမယ့် course က "Rust" အတိုင်း ကျန်နေတာက နှစ်ခုဟာ သီးခြား object တွေ ဖြစ်နေဆဲဆိုတာ သက်သေပြပါတယ်။
Course: Basic C++
Course: Advanced C++
course: Rust, other: GoRange-based for loop ထဲက reference
Range-based for ရဲ့ loop variable က element တစ်ခုစီအတွက် variable အသစ် ဖြစ်လို့ သူ့ type က element ကို ထိမလား၊ copy ကို ထိမလားကို ဆုံးဖြတ်ပါတယ်။ int price က copy ကူးပါတယ်၊ int& price က element ကို ရည်ညွှန်းပြီး ပြောင်းနိုင်ပါတယ်။ const auto& item ကတော့ copy မကူးဘဲ၊ မပြောင်းဘဲ ရည်ညွှန်းလို့ string နဲ့ တခြား element အကြီးတွေကို ဖတ်ဖို့ အကောင်းဆုံး default ပါ။
#include <iostream>
#include <string>
#include <vector>
int main() {
std::vector<std::string> tags{"sale", "new", "eco"};
for (std::string& tag : tags) tag = "#" + tag; // changes each element
for (const std::string& tag : tags) { // reads without copying
std::cout << tag << ' ';
}
std::cout << '\n'; // #sale #new #eco
}Dangling reference
Reference က သူ့ object အသက်ရှင်နေသရွေ့ပဲ ဘေးကင်းပါတယ်။ ဂန္ထဝင် အမှားကတော့ local variable ကို reference နဲ့ return ပြန်တာပါ — const std::string& greeting() { std::string text = "hello"; return text; } က function ပြီးတာနဲ့ ဖျက်ဆီးခံရမယ့် string ရဲ့ နာမည်ကို ပြန်ပေးပြီး GCC က reference to local variable 'text' returned လို့ warning ပေးပါတယ်။ အောက်က lastName() လို object က caller ပိုင်တာ ဖြစ်ရင်တော့ reference return ပြန်တာ ကောင်းပါတယ် — caller ရဲ့ vector ထဲကို reference ပြန်ပေးတာပါ။ ဒီလိုဆိုရင်တောင် vector က နောက်မှ ကြီးလာပြီး element တွေ ရွှေ့သွားရင် reference က invalid ဖြစ်သွားပါတယ်။
#include <iostream>
#include <string>
#include <vector>
std::string& lastName(std::vector<std::string>& names) {
return names.back(); // refers into the caller's vector
}
int main() {
std::vector<std::string> names{"Aye", "Bo"};
lastName(names) += " (captain)";
std::cout << names[1] << '\n'; // Bo (captain)
}သံသယရှိရင် value နဲ့ return ပြန်ပါ
Modern C++ မှာ std::string ဒါမှမဟုတ် std::vector ကို value နဲ့ return ပြန်တာ စျေးမကြီးပါဘူး — compiler က copy ကို ဖယ်ရှားပစ်တာ ဒါမှမဟုတ် move လုပ်တာ ဖြစ်လို့ပါ။ ပြန်ပေးမယ့် object က call ထက် ရှင်းရှင်းလင်းလင်း အသက်ရှည်မှသာ reference ကို return ပြန်ပါ။
လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
const T& parameter တွေက real C++ code ထဲမှာ အသုံးအများဆုံး function signature ပါ — void log(const std::string& message)၊ double total(const std::vector<Order>& orders) စသဖြင့်ပါ။ Copy ကုန်ကျစရိတ် မရှိဘဲ value လို ဘေးကင်းမှုကို (function က ကိုယ့် object ကို မပြောင်းနိုင်ဘူး) ပေးလို့ argument က record ထောင်ပေါင်းများစွာပါတဲ့ vector ဖြစ်တဲ့အခါ အရေးကြီးပါတယ်။ Function က ဖတ်ရုံပဲ ဖတ်ရက်နဲ့ std::string ဒါမှမဟုတ် std::vector parameter ကို value နဲ့ ယူထားရင် code review မှာ ပုံမှန် ထောက်ပြကြပါတယ်။
Non-const reference တွေကတော့ function ရဲ့ အလုပ်က တစ်ခုခုကို update လုပ်တာ ဖြစ်တဲ့နေရာမှာ ပေါ်လာပါတယ် — sort routine၊ output vector ကို ဖြည့်ပေးတဲ့ function၊ call တွေကို ဆက်တိုက် ချိတ်နိုင်အောင် *this ကို reference နဲ့ return ပြန်တဲ့ operator+= လို operator overload တွေပါ။ auto& ပါတဲ့ range-based for loop က container ရဲ့ element တိုင်းကို update လုပ်ဖို့ နေ့စဉ်သုံး နည်းပါ။ C++ Core Guidelines အပါအဝင် style guide အများစုက "မဖြစ်မနေ ရှိရမယ်" ဆိုရင် T& ကို၊ "မရှိလည်း ဖြစ်တယ်" ဆိုရင် T* ကို အကြံပြုပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. Reference က re-bind ဖြစ်မယ်လို့ ထင်တာ
int& r = a; ပြီးနောက် r = b; က b ရဲ့ value ကို a ထဲ copy ကူးပါတယ်။ နောက်ပိုင်း တခြား object ဆီ ပြောင်းညွှန်နိုင်တဲ့ တစ်ခုခု လိုရင် reference မဟုတ်ဘဲ pointer (ဒါမှမဟုတ် std::reference_wrapper) လိုပါတယ်။
2. Loop ဒါမှမဟုတ် parameter မှာ & မေ့တာ
for (auto item : items) ရော void update(std::vector<int> v) ရော copy တွေပေါ်မှာ အလုပ်လုပ်လို့ update တွေ တိတ်တဆိတ် ပျောက်သွားပါတယ်။ Function ဒါမှမဟုတ် loop က ပြောင်းဖို့ ရည်ရွယ်တာဆိုရင် & တကယ် ပါမပါ စစ်ပါ၊ object အကြီးကို ဖတ်ရုံပဲ ဆိုရင် copy ကို ရှောင်ဖို့ const & ထည့်ပါ။
3. push_back() ကို ဖြတ်ပြီး element reference ကို ကိုင်ထားတာ
std::string& first = names[0]; names.push_back("Zaw"); က element အားလုံးကို storage အသစ်ဆီ ရွှေ့နိုင်ပြီး first က free လုပ်ပြီးသား memory ကို ရည်ညွှန်းသွားပါတယ်။ Vector ရဲ့ size ပြောင်းပြီးရင် names[0] ကို ပြန်ဖတ်ပါ၊ ဒါမှမဟုတ် reserve() နဲ့ နေရာ ကြိုယူထားပါ။
4. Temporary ကို T& ဆီ ပို့တာ
shout(std::string("hi")) ကို cannot bind non-const lvalue reference of type 'std::string&' ... to an rvalue ဆိုပြီး compiler က ငြင်းပါတယ်။ Function က argument ကို မပြောင်းဖို့ ဆိုရင် parameter ကို temporary တွေပါ လက်ခံတဲ့ const std::string& ဖြစ်အောင် လုပ်ပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- ဖတ်ရုံပဲ ဖတ်မယ့် argument အကြီးတွေကို const T& နဲ့ ယူပြီး int၊ double လို type အသေးတွေကို value နဲ့ ယူပါ။
- Function ရဲ့ ရည်ရွယ်ချက်က argument ကို ပြောင်းဖို့ ဖြစ်မှသာ T& parameter ကို သုံးပြီး အဲဒါကို function နာမည်ကနေ ရှင်းရှင်း သိသာအောင် လုပ်ပါ။
- ဖတ်ရုံပဲ လုပ်တဲ့ range-based for မှာ const auto& ကို၊ ပြောင်းတဲ့ loop မှာ auto& ကို သုံးပါ။
- Return ပြန်မယ့် object က call ထက် ရှင်းရှင်း အသက်မရှည်ရင် value နဲ့ return ပြန်ပါ။
Mini challenge
Mini challenge: လစာ update
လစာစာရင်းအတွက် function အသေး သုံးခု ရေးပါ၊ အလုပ်တစ်ခုစီအတွက် မှန်ကန်တဲ့ parameter အမျိုးအစားကို သုံးပါ၊ ပြီးရင် return ပြန်လာတဲ့ reference ကနေ element တစ်ခုကို နေရာမှာတင် update လုပ်ပါ။
- void applyRaise(std::vector<double>& salaries, double percent) က လစာတိုင်းကို နေရာမှာတင် ပြောင်းရမယ်
- double total(const std::vector<double>& salaries) က copy မကူးဘဲ ဖတ်ရမယ်
- std::string& longestName(std::vector<std::string>& names) က အရှည်ဆုံး နာမည်ရဲ့ reference ကို ပြန်ပေးရမယ်
- ပြန်လာတဲ့ reference ကနေ " (team lead)" ကို ဆက်ထည့်ပြီး vector ထဲက element ကို print ထုတ်ရမယ်
#include <format>
#include <iostream>
#include <string>
#include <vector>
void applyRaise(std::vector<double>& salaries, double percent) {
// TODO: use a reference loop variable so each salary really changes
}
double total(const std::vector<double>& salaries) {
// TODO: add up the salaries
}
std::string& longestName(std::vector<std::string>& names) {
// TODO: find the index of the longest name and return names[index]
}
int main() {
std::vector<double> salaries{1200.0, 950.0, 1500.0};
std::vector<std::string> names{"Aye", "Thandar", "Bo"};
// TODO: print the total, apply a 10% raise, print the new total
// TODO: append " (team lead)" through longestName() and print names[1]
}ရလဒ်: Before: 3650.00 After 10% raise: 4015.00 Thandar (team lead)
အရိပ်အမြွက် ကြည့်ရန်
applyRaise ထဲမှာ for (double& salary : salaries) က ပြောင်းလဲမှုကို တကယ် တည်မြဲစေတာပါ။ longestName() က local copy ကို မဟုတ်ဘဲ caller ရဲ့ vector ထဲက element ဖြစ်တဲ့ names[best] ကို return ပြန်ရပါမယ်။
အဖြေ ကြည့်ရန်
#include <format>
#include <iostream>
#include <string>
#include <vector>
void applyRaise(std::vector<double>& salaries, double percent) {
for (double& salary : salaries) {
salary *= 1.0 + percent / 100.0;
}
}
double total(const std::vector<double>& salaries) {
double sum = 0.0;
for (double salary : salaries) sum += salary;
return sum;
}
std::string& longestName(std::vector<std::string>& names) {
std::size_t best = 0;
for (std::size_t i = 1; i < names.size(); ++i) {
if (names[i].size() > names[best].size()) best = i;
}
return names[best];
}
int main() {
std::vector<double> salaries{1200.0, 950.0, 1500.0};
std::vector<std::string> names{"Aye", "Thandar", "Bo"};
std::cout << std::format("Before: {:.2f}\n", total(salaries));
applyRaise(salaries, 10.0);
std::cout << std::format("After 10% raise: {:.2f}\n", total(salaries));
std::string& star = longestName(names);
star += " (team lead)";
std::cout << names[1] << '\n';
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Reference က ရှိပြီးသား object ရဲ့ နောက်ထပ် နာမည်ပါ — initialize မဖြစ်မနေ လုပ်ရပြီး ဘယ်တော့မှ re-bind လုပ်လို့ မရပါဘူး။
- Reference ကို assign လုပ်တာက သူ ခေါ်တဲ့ object ကို assign လုပ်တာပါ။
- ဖတ်ရုံပဲ ဖတ်မယ့် argument အကြီးတွေကို const T& နဲ့ ပို့ပါ၊ ပြောင်းဖို့မှသာ T& သုံးပြီး type အသေးတွေကို value နဲ့ ပို့ပါ။
- Object က အရင် သေသွားရင် reference က dangle ဖြစ်လို့ local variable ရဲ့ reference ကို ဘယ်တော့မှ return မပြန်ပါနဲ့။
C++ Core Guidelines — Standard C++ Foundation