const là gì - Nghĩa của từ const
Ngày đăng:
07/04/2022
Trả lời:
0
Lượt xem:
170
const có nghĩa làMột phiên bản rút ngắn của các từ "hằng số" và / hoặc "liên tục." Ví dụ"Bạn biết đấy, bây giờ tôi thực sự chú ý đến bản thân mình, tôi thấy mình const sửa chữa ngữ pháp của mình. Loại khó chịu, không?""Tôi thích cách chúng const có nghĩa làPhiên bản người say rượu của Boosted Ví dụ"Bạn biết đấy, bây giờ tôi thực sự chú ý đến bản thân mình, tôi thấy mình const sửa chữa ngữ pháp của mình. Loại khó chịu, không?"const có nghĩa làThe hidden *this pointer that's present in every member function of a class (at least, in C++). Ví dụclass Something{ int a,b; public: Something(int a, int b): this->a(a), this->b(b) {} void doSomething() {} // is translated into "void doSomething(Something* const this) {}" // The T in "T* const this" is replaced with the class type friend void doSomething2() {} // is not translated, as it is NOT a member function }; void doSomething2() {} const có nghĩa làvoid Dos Something () {} // được dịch thành "Void Dos Something (một cái gì đó * const này) {}" Ví dụconst int c{5}; // uniform initilization// now c is const, any attempt to modify it results in an error c = 6; // error, c is const int d; d = c; // fine, we're not modifing c // another example: class A { int a; public: A(): a(5) {} void incrementA() { ++a; } void incrementA2() const { ++a; } // error, incrementA2 is const, and will not modify a }; |