[C++] const object

const  object

  • const 对象只能调用const函数
  • const函数不能改变一般成员变量的值,但是mutable的变量不受限制

时间: 2024-08-24 18:23:25

[C++] const object的相关文章

引用和const的一些

一,引用:某个变量的引用等价于这个变量,是这个变量的一个别名. #include<iostream> int main() { int a=2,b=1; int &c=a;//(1)定义引用时一定要初始化 c=3; std::cout<<a<<std::endl; c=b; //此时并不是c引用了b而是c(实际上是a变量的值)的值变为了b变量的值,并不能改变c的引用 //(2)引用一经初始化就不会再引用其他的变量了. std::cout<<a<

顶层const和底层const

As we’ve seen, a pointer is an object that can point to a different object. As a result,we can talk independently about whether a pointer is const and whether the objectsto which it can point are const. We use the term top-level const to indicate tha

const和readonly

const:常量,编译时即需要确定值 readonly:只读变量,运行时确定值 1 class ConstReadonlyTest 2 { 3 //const String str;//错误:常量字段定义时需要指定初始值 4 //const Object obj = new Object();//错误:常量字段不能使用new初始化,表达式必须是常量(字符串可以) 5 //const Object obj = new StructTest();//错误:表达式必须是常量不能使用new初始化(即使是

第2章 变量和基本类型 附3---底层const和顶层const

和英文版的对: As we’ve seen, a pointer is an object that can point to a different object. As a result,we can talk independently about whether a pointer is const and whether the objectsto which it can point are const. We use the term top-level const to indi

【C++注意事项】5 Top-level const , The auto and decltype Type Specifier

top-level const As we've seen, a pointer is an object that can point to a different object. As a result, we can talk independently about whether a pointer is const and whether the objects to which it can point are const. we use the top-level const to

C++ const用法总结

一.关于一般常量 声明或定义的格式如下: const <类型说明符> <变量名> = <常量或常量表达式>; [1] <类型说明符> const <变量名> = <常量或常量表达式>; [2] [1]和[2]的定义是完全等价的. 例如: 整形int(或其他内置类型:float,double,char) const int bufSize = 512; 或者 int const bufSize = 512; 因为常量在定义后就不能被修改

C++ 之const Member Functions

Extraction from C++ primer 5th Edition 7.1.2 The purpose of the const that follows the parameter list of an ordinary member function is to modify the type of the implicit this pointer. By default, the type of this is a const pointer to the nonconst v

条款3:尽可能使用const(use const whenever possible)

1.只要这(某值保持不变)是事实,就应该确实说出来,这样可以获得编译器的协助,确保这条约束不被违反. 2.keyword const 有很多种用法,但都简单易用. 2.1classes 外部修饰global/namespace作用域中的常量. 2.2修饰文件.函数.区块作用域中被声明的static 对象. 2.3修饰classes内部的static和non-static 成员变量. 2.4对于指针,可以指出ptr自身.ptr所指物或者两者都(或都不)是const. 这里不清楚static跟 co

effective c++ 条款3 use const whereever you can

1 const 传达的意思应该是这个变量是常量不能更改 2 const 在 * 左边表示数据是const,在右边表示指针是const // char greeting[] = "hello"; char* p = greeting; //const *: const data //* const: const pointer char const *p1 = greeting; // const data char * const p2 = greeting; // const poi