C++基础知识学习,static详解,类的标准写法,类的隐式类型转换,构造函数初始化列表,const函数

声明文件的类与变量的不同

.h文件中,类的声明文件(包含防卫式头)可以在多个cpp文件中引用 但是如果声明的是变量,多个cpp文件都include的话,一定会报错 不要问为什么,就是这样设计的

从第一个有默认值的参数开始,之后的参数都必须有默认值

在这里插入图片描述

类通过构造方法的隐式转换

如果一定义一个类,像通过这样一种方式直接初始化类
className abc = 12
那么你可以通过构造方法实现

testClass::testClass(int a)
{

}

int main()
{
	//默认会去调用带一个int参数的构造方法
	testClass abc = 12;
	//这样声明的话,也是会去调用一个int参数的构造方法
	//即使你有两个int类型的构造方法,我也不知道为什么
	testClass abc2 = (2, 3);
}
如何明确构造方法不能被隐式转换

明确构造函数不能进行隐式转换,方法前面加上explicit 单参数的构造方法,都要声明explicit,除非特殊情况
testClass.h

explicit testClass(int a);

testClass.cpp

//这里面不需要加上explicit
testClass::testClass(int a)
{

}
构造函数初始化列表
testClass(int d, int e, int f);
testClass::testClass(int d, int e, int f) :a(d), b(e), c(f) 
{

}
直接在类定义种(.h)实现的函数会被当成inline函数来处理
#ifndef __TESTCLASS__H__
#define __TESTCLASS__H__
class testClass
{
private:
	int a;
	int b;
	int c;
public:
	testClass();
	testClass(int a);
	testClass(int d, int e, int f);
	//这个函数直接在这个.h声明文件中定义了方法内容,把么调用的时候
	//就会把它当成inline函数,尝试用内容去替换调用
	void testMethod1()
	{
		cout << "hello world" << endl;
	}
};
#endif 
const函数

函数末尾的const,不但要在声明中添加,定义中也添加
作用:告诉系统该函数不会修改对象里面的任何变量的值,修改会报错(必须是能修改的值)

  • const结尾的函数,可以被任何对象调用,const函数不能放在普通函数里面,因为根本没有对象
  • 声明成为普通函数的对象,不能被const对象调用
void testMethod2() const;
void testClass::testMethod2() const
{
	//如果const函数里面使用了非cosnt对象,就会报错
}
void testMethod1();
void testClass::testMethod1()
{
}
int main()
{
	testClass abc;
	abc.testMethod2();//这是对的
	abc.testMethod1();//这是对的
	const testClass abc2;
	abc2.testMethod2();//这是对的
	abc2.testMethod1();//这是错的,const对象不能调用非const方法
}
返回自身对象的引用this
testClass& getMySelf();
testClass& testClass::getMySelf()
{
	//这是固定写法,就是返回*this
	return *this;
}

解释:
编译器实际上会把class&this,当作第一个参数,任何对类成员的操作都会被当作this.file/func
就像是这样:
testMethod(className* const this,type otherFiles),this不能改变指向
由此可推,cosnt函数中:
testMethod (const className * const this,type others) const{}

应用:
有了这个返回this的方法,就可以使用build模式了

static详解

static局部变量
void testMethod2()
{
	//每一次退出去的时候会默认保存counter,下一次接着计算
	static int counter = 0;
	//TODO....
	counter++;
}
int main()
{
	//static成员
	testMethod2();
}

如果只是声明了一个static变量,那么这个变量只能在本文件中使用

static int testStatic = 1;
int main()
{
	cout<<testStatic<<endl;
}

如果没有声明static,那么别的文件中使用extern int name即可在别的文件中引用
testClass.cpp

int abc = 2;

main.cpp

extern int abc;

int main()
{
	cout << abc << endl;
}
如何声明类的静态变量

1、在.h声明文件中声明

#ifndef __TESTCLASS__H__
#define __TESTCLASS__H__
class testClass
{
public:
	static int testStatic;
};
#endif 

2、在一个cpp文件中进行初始化,只能在一个cpp文件中进行初始化,否则报错“重复定义”

int testClass::testStatic = 1;
int main()
{
	cout << testClass::testStatic << endl;
}
静态成员函数

静态成员函数中不能使用非静态成员变量,因为静态成员函数是与类相关的,和对象关系不大,
或者你如果引用了 clssName::staticFunc(){},这样引用的话,根本无法判断对象变量
如下的例子:

类声明文件

#ifndef __TESTCLASS__H__
#define __TESTCLASS__H__
class testClass
{
public:
	static int testStatic;
	static void testMethod3();
};
#endif 

类定义文件

#include "testClass.h"
void testClass::testMethod3()
{
	testStatic = 2;
}

main.cpp

int testClass::testStatic = 1;
int main()
{
	testClass::testMethod3();
	cout << testClass::testStatic << endl;
}

运行结果如下:
在这里插入图片描述

发布了157 篇原创文章 · 获赞 167 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_40666620/article/details/102798846