day5 C++ 类中的const static

const

1、const修饰数据成员
与类外部数据一致,即认为此数据成员不可改变。
在参数列表(构造函数)位置初始化

class A{
public:
A(int a):num(a){}//参数列表初始化const数据成员
private:
const int num;
};

2、const修饰成员函数
首先,声明方式:const位于函数声明之后,函数体之前,如果声明在头文件和实现源文件都需要加const,即括号之前的位置,!!!注意与const返回类型区分。

class A{
public:
A(int a):num(a){}//参数列表初始化const数据成员
void func()const{}//const函数
private:
const int num;
};

意义: 修饰成员函数即宣称此函数不会改变数据成员(包括非const),因此此函数如果需要调用其他函数也只能调用const函数,才能保证不改变数据成员。

最后const构成重载,这也是为什么声明和实现都要加const的原因。const对象只能调用const成员函数,非const对象优先调用非const对象。

3、const修饰类对象。
意义:从对象层面宣称此对象不修改数据,只能调用const函数。

static

1、在C语言中:
修饰全局变量使得此变量作用域仅限于本文件,
修饰局部变量使得此变量生命周期与main函数相同,并且存储位置改变不在堆栈,处于数据段rw段

2、在类内部中:
初始化位置:
类内声明,类外初始化:

class A{
 public:
void func(int num){
      a=num;
}
void dis(){
cout<<a;
}
private:
static int a;
};
 int A:: a=10;//初始化
 
int main(){
    A Q,W,E;
    Q.func(20);
    Q.dis();
    W.func(30);
    Q.dis();
    E.func(40);
    Q.dis();
}

static用来实现族类对象间数据共享,在生成对象的普通数据成员才拥有空间,而static成员在类声明时就开辟了空间。
因为数据共享,因此上述代码的三个Q.dis()的输出结果是不一样的。
在这里插入图片描述

3、因此没有对象也能访问static数据成员,类也是一个命名空间,但这里我修改了a的权限为public 不然无法访问。
在这里插入图片描述
4、static修饰成员函数只有一个作用:管理static数据成员。
static成员函数无this指针,因此它没有办法访问对象的其他数据成员。
static成员函数与数据成员既属于对象也属于类,但说到底是属于类的。因此static成员不占用对象空间,属于类空间。

5、static const 同时修饰一个成员,即共享且不可改变,初始化方式为就地初始化。

写在最后

类本质也是一个命名空间。
static成员函数与数据成员既属于对象也属于类,但说到底是属于类的。因此static成员不占用对象空间,属于类空间。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是基于你提供的类定义的C++代码: ``` #include <iostream> #include "Date.h" using std::ostream; // Define static member days. const int Date::days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // Constructor: initialize month, day, year. Date::Date(int m, int d, int y) { setDate(m, d, y); } // Set the date. void Date::setDate(int m, int d, int y) { month = (m >= 1 && m <= 12) ? m : 1; year = (y >= 1900 && y <= 2100) ? y : 1900; // Test for a leap year. if (month == 2 && leapYear(year)) day = (d >= 1 && d <= 29) ? d : 1; else day = (d >= 1 && d <= days[month]) ? d : 1; } // Prefix increment operator. Date& Date::operator++() { helpIncrement(); return *this; } // Postfix increment operator. Date Date::operator++(int) { Date temp = *this; helpIncrement(); return temp; } // Add days to the date. const Date& Date::operator+=(int additionalDays) { for (int i = 0; i < additionalDays; i++) helpIncrement(); return *this; } // Determine if the year is a leap year. bool Date::leapYear(int testYear) const { if (testYear % 400 == 0 || (testYear % 100 != 0 && testYear % 4 == 0)) return true; else return false; } // Determine if the date is at the end of the month. bool Date::endOfMonth(int testDay) const { if (month == 2 && leapYear(year)) return testDay == 29; else return testDay == days[month]; } // Utility function to increment the date. void Date::helpIncrement() { if (!endOfMonth(day)) day++; else { if (month < 12) month++; else { month = 1; year++; } day = 1; } } // Overloaded stream insertion operator. ostream& operator<<(ostream& output, const Date& d) { static char* monthName[13] = { "", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; output << monthName[d.month] << ' ' << d.day << ", " << d.year; return output; } ``` 可以在代码中添加其他函数和运算符的实现以满足你的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值