示例1、看注释读懂c++中的const
#include <iostream>
using namespace std;
int d = 10; //全局变量,其实实质是extern int d = 10;默认将extern省略。全局变量存储在静态区,生存期为整个程序生存期
static int dd = 10; //加static表示,该全局变量作用域限制在本cpp文件,不能被其他文件看到,变量存储在静态区,生存期为整个程序生存期
const int a = 10; //真常量,一旦定义不能被修改,因此定义的时候需要赋初始值,在编译时期初始化
const int c = d; //在运行时初始化
class A{
public:
A(int x = 0, int y = 0, int z = 0)
:real(x), image(y), tet(z) //const 成员必须在初始化列表中初始化
{};
void func() const //const函数表示,该函数体内的内容不能修改,const函数和非const函数可以构成重载,
{ //非const对象可以调用const成员函数,但是如果有同函数名的非const函数重载,
cout << real << image << tet << endl; //则调用非const成员函数,如func()const 和func()构成了重载,如果非const对象a调用func(),
cout << "in the void func const" <<endl; //那么调用的是非const的func,如果没有非const的func重载,a才会去调用func()const;
cout << real << endl; //const成员函数中,可以有非const的成员,但是不能修改,否则报错
//real = 10; //报错
//show(); //const成员函数中,不能调用非const成员函数,因为不能确定该成员函数有没有修改数据。
}
void func(){
cout << "in the void func" << endl;
}
void func1() const
{
cout << "func1: "<< real << image << tet << endl;
}
void show(){ //const成员和const成员函数,可以被非const成员函数调用
cout << real << image << tet << endl;
func1();
func(); //当有const重载时,非const成员函数优先调用非const函数。
}
int pubx;
const int puby = 0; //必须要初始化,因为,以后不能更改;初始化方式,可以像常量一样直接赋值,也可以通过初始化列表进行
private:
int real;
int image;
const int tet = 10;
};
int main(void)
{
A a(1,2,3);
a.func();
a.show();
cout << a.puby << endl;
const A b(2,3,4); //const对象只能调用const成员函数
b.func();
//b.show(); //报错,const对象只能调用const成员函数
cout << b.pubx << endl; //可以使用非const成员,
//b.pubx = 0; //但是不能为非const成员赋值
return 0;
}
示例2、读懂c++中的static
#include <iostream>
using namespace std;
/*
static int m = 10; //static修饰全局变量,表示全局变量的作用域限制在该文件之内,默认初始化为0,静态区本身就都是'\0'
void func(){
static int dt = 0; //static修饰局部变量,表示该局部变量的生存周期为整个程序生存周期,但是作用域依然是该函数之内
//事实上赋值语句从来没有执行过,程序加载过错中,就已经赋值完成, 默认初始化为0。
dt++;
cout << dt << endl;
}
static void func2(){ //static 函数和一般函数的区别是,static函数的作用域为本文件,且在内存中只有一份,普通函数在每个调用者中维持一个拷贝
cout << m << endl;
}
void show1()
{
func();
func2();
cout << (void*)func << endl;
cout << (void*)func2 << endl;
}
void show2()
{
func();
func2();
cout << (void*)func << endl;
cout << (void*)func2 << endl;
}
int main(void)
{
func(); //第一次调用输出1
func(); //第二次调用输出2,静态变量只会初始化一次,而且其生存周期是整个程序,不过器作用域和一般变量一样
show1();
show2();
}
*/ //以上static在C语言中的特性, 在C++中同样适用,引入类之后,static也在类中进行了扩展
void func()
{
int a = 0;
cout << a << endl;
}
class A{
public:
A(int x, int y, int z):x(x),y(y){ //好的习惯,不管是通过重载构造器还是默认参数的方式,需要保证默认构造器有效,不然创建对象数组的时候容易出错
//this->x = x;
//this->y = y;
//this->z = z;
//this->m = x;
}
int getz()
{
return z;
}
static void show()
{
//cout << x << y << z << endl; //错误, 静态成员函数只能访问静态成员
cout << z++ <<endl;
//show2(); //错误,静态成员函数直接通过类访问,也可以通过对象调用,它只能访问静态变量,其他静态函数和类外部的其他函数。
show3(); //可以调用其他静态函数,
func(); //和类外部的其他函数。
}
void show2() //普通函数可以操作静态成员,但是静态函数不能操作普通成员
{
cout << z << endl;
}
static void show3() //因为静态成员函数,和对象无关,与类直接相关;因此它的函数体中,不能使用非static成员。非static成员是和对象相关的
{ //如果static成员函数可以访问非static成员,将造成混乱,不知道访问哪个对象的成员。
cout << z << endl; //静态成员函数没有this指针
}
static int m; //属于类
private:
int x;
int y;
static int z; //静态成员可以是私有的,属于类,直接用A::z = 2;来进行初始化
};
int A::m = 10; //初始化,不能写为static int A::m = 10;这一点和const不同,
int A::z = 2; //私有化的static成员,也是通过类来赋值。
int main(void){
A a(1,2,3);
A::m = 10;
A::show(); //通过类名来访问
A::show();
a.show(); //也可以通过对象来访问
a.show();
a.m = 0; //m是public,对象可以直接访问;
cout << a.getz() << endl; //z为private,对象不能直接访问,需要借助成员函数进行访问
return 0;
}