C++知识点14——类与static

目前为止,提到的所有的类内成员都是与对象相关的,就是类中的成员被每个对象分别持有,对象的地址通过this指针来区分

但是,类中的有一种成员不是与对象相关联,而是与类的整体相关联,这种成员就是static成员

因为static成员函数不为类的对象所有,所以,static成员函数没有this指针,所以,不能将static成员函数声明为const,也不能在static成员函数中调用非static成员变量,更不能在static成员函数中使用this指针

class staticwithclass
{
public:
	staticwithclass(){}
	~staticwithclass(){}

	int i;
	static void func() const;
};

不能将static成员函数声明为const,因为const将this指针指向的对象变为const,而static成员函数无this指针

 

class staticwithclass
{
public:
	staticwithclass(){}
	~staticwithclass(){}

	int i;
	static void func();
};

void staticwithclass::func()
{
	i=10;
}

不能在static成员函数中调用非static成员变量,static关键字在成员函数声明时添加就可以,在成员函数定义时不能重复添加

 

void staticwithclass::func()
{
	this->i=10;
}

不能在static成员函数中使用this指针

 

虽然static成员不属于类的对象,但是依然可以用类的对象,指针和引用来访问静态成员

class staticwithclass
{
public:
	staticwithclass(){}
	~staticwithclass(){}

	int i;
	//static int a;
	static void func();
};
void staticwithclass::func()
{
	cout<<__func__<<endl;
}

int main(int argc, char const *argv[])
{
    staticwithclass::func();
	staticwithclass t;
	t.func();
	staticwithclass *p=new staticwithclass;
	p->func();
	staticwithclass &r=t;
	r.func();
	return 0;
}

 

因为static成员变量不属于任何对象,所以在类的对象创建的时候,无法初始化static成员,所以静态成员变量一般情况下放在类的外边定义并初始化

class staticwithclass
{
public:
	staticwithclass(){}
	~staticwithclass(){}

	int i;
	static int a;
	static void func();
};

int staticwithclass::a=10;

void staticwithclass::func()
{
	cout<<__func__<<endl;
	a=100;
	cout<<a<<endl;
}

int main(int argc, char const *argv[])
{
	staticwithclass::func();
	return 0;
}

 

如果想在类内初始化static成员变量,那么必须用const修饰该变量

class staticwithclass
{
public:
	staticwithclass(){}
	~staticwithclass(){}

	int i;
	static const int a=10;
	static void func();
};

void staticwithclass::func()
{
	cout<<__func__<<endl;
	cout<<a<<endl;
}

int main(int argc, char const *argv[])
{
	staticwithclass::func();
	return 0;
}

 

如果不加const 会报错

此外static成员的类型可以是类的类型,而非static成员只能使用类的指针,详细见C++知识点13——友元,类的声明

class staticwithclass
{
public:
	staticwithclass(){}
	~staticwithclass(){}
	static staticwithclass t; //如果将static去掉,则不能编译通过
};

 

参考:

《C++ Primer》

 

欢迎大家评论交流,作者水平有限,如有错误,欢迎指出

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在面向对象的编程中,C语言并不直接支持和抽象的概念。引用中提到,final关键字用来修饰方法,表示该方法不能在子中被覆盖。而abstract关键字用来修饰抽象方法,表示该方法必须在子中被实现。然而,在C语言中,没有对应的关键字来实现和抽象的概念。 相反,C语言通过结构体来模拟的概念。结构体是一种用户自定义的数据型,可以包含多个不同型的数据成员。通过结构体,我们可以将相关的数据和功能组合在一起。然而,C语言中的结构体不支持继承和多态等面向对象的特性。 在C语言中,我们可以使用函数指针来模拟抽象和接口的概念。函数指针可以指向不同的函数,通过使用函数指针,我们可以实现多态性,即在运行时根据函数指针指向的具体函数来执行不同的操作。 综上所述,C语言并不直接支持面向对象中的和抽象的概念,但可以使用结构体和函数指针来实现似的功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [面向对象——对象](https://blog.csdn.net/shouyeren_st/article/details/126210622)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [面向对象编程原则(06)——依赖倒转原则](https://blog.csdn.net/lfdfhl/article/details/126673771)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值