C++ static const 和 static const

1:static成员函数是类的组成部分,但不是任何对象的组成部分,因此static成员函数没有this指针(成员函数的隐含实参);

     static成员函数也不能声明为const;但是static变量可以被声明为const;

     static成员函数可以直接访问所属类的static成员,但不能直接访问所属类的非static成员。

 

test_1.h 

class test_1
{
public:
	//test_1();  //由于存在const成员,因此构造函数必须使用初始化参数列表
	test_1(int x) :c_wid(x) { }
	~test_1();

private:
	//static int s_len = 5;  //static静态成员变量不能在类内部初始化
	static int s_len;
	//const  int c_wid = 5;  //使用无误,但是不推荐,因为基于类的所有对象的这个成员变量值都一样,失去了对象的不同特性
	const  int c_wid;
	static const int sc_area;
	//const static int sc_area;  //与上一行代码static const效果一样
};

test_1.cpp

#include "test_1.h"

//static int test_1::s_len = 3;  //static关键字只能用于类内部的声明中,定义时不能标示为static
int test_1::s_len = 3; 
const int test_1::sc_area = 15;

test_1::~test_1()
{
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值