error C2131: 表达式的计算结果不是常数

 #include <iostream>
using namespace std; void main()
{
const int class_size = ;
int finalGrade[class_size]; int a = ;
int arr[a];//error C2131: 表达式的计算结果不是常数 int x;
std::cin >> x;
const int size = x;
double classAverage[size];//error C2131: 表达式的计算结果不是常数 system("pause");
}

指向常量的指针

const int * p;

常量指针

int x=5;

int * const p=&x;

指向常量的常量指针

int x=2;

const int * const p=&x;

error C3892: “s1”: 不能给常量赋值

 #include <iostream>
using namespace std; void main()
{
const char * s1 = "hello world";
char s2[] = "hello world"; std::cout << s1 << std::endl;
std::cout << s2 << std::endl; s1[] = 'a';//error C3892: “s1”: 不能给常量赋值
s2[] = 'a'; std::cout << s1 << std::endl;
std::cout << s2 << std::endl; printf("s1=%p\n", &s1);
printf("s2=%p\n", &s2); system("pause");
}

const对象

1常量成员

2常引用作为函数参数

3常对象

类名 const 对象名(参数表);//必须初始化

4常成员函数

类型标识符 函数名(参数列表) const;

 #include <iostream>
using namespace std; class A
{
const int i;
public:
A() :i()
{ }
void f()
{
std::cout << "f()" << std::endl;
}
void f() const
{
std::cout << "f() const" << std::endl;
}
}; void main()
{
const A a;
a.f(); A a1;
a1.f(); system("pause");
}

在常成员函数里,不能更新对象的数据成员,也不能调用该类中没有用const修饰的成员函数。如果将一个对象说明为常对象,则通过该对象只能调用它的const成员函数,不能调用其它成员函数。

面向对象程序设计-C++_课时19const_课时20不可修改的的更多相关文章

  1. 面向对象程序设计-C++_课时18内联函数

    使用inline说明的函数称内联函数. 在C++中,除具有循环语句.switch语句的函数不能说明为内联函数外,其他函数都可以说明为内联函数. #include <iostream> us ...

  2. 面向对象程序设计-C++_课时30运算符重载——基本规则_课时31运算符重载——原型_课时32运算符重载——赋值_课时33运算符重载——类型转换

    区分初始化,赋值 #include <iostream> using namespace std; class Fi { public: Fi() {}//1构造函数 }; class F ...

  3. 面向对象程序设计-C++_课时28静态对象_课时29静态成员

    Static in C++ Two basic meanings Static Storage --allocated once at a fixed address Visibility of a ...

  4. 面向对象程序设计-C++_课时26拷贝构造Ⅰ_课时27拷贝构造Ⅱ

    一旦写了一个类,给它3个函数: 1default construtor 2virtual destructor 3copy constructor Constructions vs. assignme ...

  5. 面向对象程序设计-C++_课时24多态的实现

    所有带virtual的类的对象,里面最上面有一个隐藏的指针vptr,指向一张表vtable #include <iostream> using namespace std; class A ...

  6. 面向对象程序设计-C++_课时22向上造型

    赋值兼容规则是指在公有派生情况下,一个派生类的对象可以作为基类的对象来使用的情况. 约定类derived是从类base公有派生而来的,则指如下3种情况: (1)派生的对象可以赋给基类的对象.例如: d ...

  7. 面向对象程序设计-C++_课时21引用

    数据类型 & 别名=对象名; #include <iostream> using namespace std; int * f(int * x) { (*x)++; return ...

  8. 面向对象程序设计-C++_课时17函数重载和默认参数

    函数重载,区别一是参数类型不同,二是参数个数不同. 默认参数可以多于1个,但必须放在参数序列的后部. 尽量不要用默认参数,会影响阅读 error C2668: “f”: 对重载函数的调用不明确 #in ...

  9. 面向对象程序设计-C++_课时16子类父类关系

    初始化列表 类名::类名(形参1,形参2,...形参n):数据成员1(形参1),数据成员2(形参2),...,数据成员n(形参n) { ... } 规则1,初始化列表进行数据成员的初始化 规则2,初始 ...

随机推荐

  1. php 数组去除空值

    /** * 方法库-数组去除空值 * @param string $num 数值 * @return string */ public function array_remove_empty(& ...

  2. jchat:linux聊天程序1:简介

    做一个linux的聊天软件,虽然没什么创意,但是它可以用来锻炼和测试我对网络编程的掌握程度,也借此机会做一些有意思的程序. 这里做的是linux下一个命令行的客户端与服务器的聊天程序,没写界面,因为对 ...

  3. J2SE知识点摘记(六)

    1.        static关键字的使用 static 关键字:可以用于修饰属性,也可以用于修饰方法,还可以用于修饰类. static 修饰属性:无论一个类生成了多少个对象,所有这些对象共同使用唯 ...

  4. The solution for "Eclipse is running in a JRE, but a JDK is required"

    Open the eclipse folder and access the eclipse.ini file:   Before change it ,you will find it don’t ...

  5. Mono For Android中AlarmManager的使用

    最近做了一个应用,要求如下: 程序运行之后的一段时间,分别触发3个不同的事件.当然很快就想到了Android中的AlarmManager和BroadcastReceiver.但是毕竟Mono环境和Ja ...

  6. Type Correlation

    Types of correlation: Logical correlation: Using pre-defined and customized correlation rules. Inven ...

  7. /dev/console,/dev/null,/dev/tty

    UNIX和Linux中比较重要的三个设备文件是:/dev/console,/dev/tty和/dev/null. 0 :  /dev/console 这个设备代表的是系统控制台,错误信息和诊断信息通常 ...

  8. CSS实现页面背景自动切换功能

    From here:http://xiaomiya.iteye.com/blog/2047728 请看效果图: 完整代码如下: <!DOCTYPE HTML> <html> & ...

  9. C/C++各种系统开发环境搭建

    http://pan.baidu.com/s/1qWJKF4g

  10. C# linq to xml

    XDocument doc = new XDocument( new XDeclaration("1.0", "utf-8", "yes") ...