zoukankan      html  css  js  c++  java
  • C++笔记(一)

    关于const

    int i = 0;
    
    // 以下两种声明等同,int类型的常量
    const int a = 1;
    int const b = 1;
    
    // 指向常量int类型的引用
    const int &c = b;
    // 指向int类型的常量引用
    int &const d = i;
    // 指向常量int类型的常量引用
    const int &const e = a;
    
    // 指向常量int类型的指针
    const int *f = &a;
    // 指向int类型的常量指针
    int *const g = &i;
    // 指向常量int类型的常量指针
    const int *const h = &b;
    
    typedef int *type_int;
    // 以下声明等同,都是int类型的指针常量
    const type_int l;
    type_int const m;
    int *const n = &a;
    
    class Test
    {
    	int a;
    public:
    	void test()
    	{
    		cout << "hello" << endl;
    	};
    	// 常量成员方法:
    	// 1、不能修改成员变量;
    	// 2、不能调用非const方法;
    	// 3、可以被其他成员方法调用;
    	void show() const
    	{
    		// error
    		test();
    		
    		// error
    		a = 2;
    
    		// correct, 可以读取成员变量的值,就像可以读取const对象的值一样
    		cout << a << endl;
    	};
    }
    


  • 相关阅读:
    hdu3487 Play with Chain
    poj3481
    [HNOI2002]营业额统计
    poj3468 A Simple Problem with Integers
    [NOI2004]郁闷的出纳员
    UVa1308 LA2572
    20130620
    poj3580
    20130618
    C++类模版学习笔记
  • 原文地址:https://www.cnblogs.com/zhanghang-BadCoder/p/6476461.html
Copyright © 2011-2022 走看看