tailieunhanh - Thinking in C plus plu (P9)
Tham khảo tài liệu 'thinking in c plus plu (p9)', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | int main cout sizeof Bunch sizeof Bunch sizeof i 1000 sizeof int 1000 endl The use of enum here is guaranteed to occupy no storage in the object and the enumerators are all evaluated at compile time. You can also explicitly establish the values of the enumerators enum one 1 two 2 three With integral enum types the compiler will continue counting from the last value so the enumerator three will get the value 3. In the above the line static const int size 100 would be instead enum size 100 Although you ll often see the enum technique in legacy code the static constfeature was added to the language to solve just this problem. However there is no overwhelming reason that you must choose static constover the enum hack and in this book the enum hack is used because it is supported by more compilers at the time this book was written. const objects member functions Class member functions can be made const What does this mean To understand you must first grasp the concept of const objects. A const object is defined the same for a user-defined type as a built-in type. For example const int i 1 const blob b 2 380 Thinking in C www. BruceEckel .com Here b is a const object of type blob. Its constructor is called with an argument of two. For the compiler to enforce constness it must ensure that no data members of the object are changed during the object s lifetime. It can easily ensure that no public data is modified but how is it to know which member functions will change the data and which ones are safe for a const object If you declare a member function const you tell the compiler the function can be called for a const object. A member function that is not specifically declared const is treated as one that will modify data members in an object and the compiler will not allow you to call it for a const object. It doesn t stop there however. Just claiming a member function is const doesn t guarantee it will act that way so the compiler forces you to .
đang nạp các trang xem trước