一、在类定义中实现成员函数inline
在.h头文件中声明类,如下:
#ifndef __MAIN_H__
#define __MAIN_H__
class student
{
public:
int age;
int hight;
int num;
void stuInitFunc(int stuAge, int stuHight, int stuNum);
};
#endif // !__MAIN_H__
在.cpp文件中定义类,如下:
void student::stuInitFunc(int stuAge, int stuHight, int stuNum)
{
this->age = stuAge;
this->hight = stuHight;
this->num = stuNum;
}
在main函数或者其它函数调用student成员的stuInitFunc函数如下:
int main()
{
student stu;
stu.stuInitFunc(20,180,1001);
cout << stu.age<< " " << stu.hight << " " << stu.num << endl;
return 0;
}
以上的写法是我们平时的写法,如果我们把函数的整个定义都写在.h头文件中,整个程序如下所示,.h头文件的声明如下:
#ifndef __MAIN_H__
#define __MAIN_H__
class student
{
public:
int age;
int hight;
int num;
void stuInitFunc(int stuAge, int stuHight, int stuNum)
{
this->age = stuAge;
this->hight = stuHight;
this->num = stuNum;
}
};
#endif // !__MAIN_H__
在.cpp文件中调用类的成员函数如下所示:
#include#include #include #include"main.h" using namespace std; int main() { student stu; stu.stuInitFunc(20,180,1001); cout << stu.age << " " << stu.hight << " " << stu.num << endl; return 0; }
在类的声明中,这种直接把成员函数定义写在类声明里面,会被当成inline内联函数来处理。
二、成员函数末尾的const
这里再介绍一种const的常用用法,就是在成员函数的末尾增加一个const。对于成员函数的声明和定义代码分开的情况下,不仅要在成员函数的声明中增加const,也要在成员函数的定义中增加const。这样做的作用是告诉系统,在这个成员函数中不能修改该对象里面的任何成员变量值。
我们在成员函数声明后面加上const,如下:
void stuInitFunc(int stuAge, int stuHight, int stuNum) const;
成员函数定义时,在参数圆括号后面加上const,如下:
void student::stuInitFunc(int stuAge, int stuHight, int stuNum) const
{
this->age = stuAge;
this->hight = stuHight;
this->num = stuNum;
}
编译后,语法出现错误,报错信息如下所示:
三、mutablemutable,翻译成中文,表示不稳定的、容易改变的意思。与 const 正好是反义词。而且
mutable的引入也正是为了突破const 目的限制。
在成员函数末尾加const,是不允许修改成员变量值的。那在设计类成员变量的时候,如果确实遇到了需要在const结尾的成员函数中希望修改成员变量的需求,怎么办?有人提出,把成员函数的末尾的const去掉,变成一个不以const结尾的成员函数。那这个时候可能面临一个问题:如果这个成员函数从const变成非const了,那么就不能被const对象调用了。所以,引入mutable修饰符(关键字)来修饰一个成员变量。一个成员变量一旦被mutable所修饰,就表示这个成员变量永远处于可修改状态,即使是在以const结尾的成员函数中。
我们在上面的例子类声明过程中,在声明成员变量前面加上mutable关键字,在成员函数就可以修改成员变量的值,修改如下:
class student
{
public:
mutable int age;
mutable int hight;
mutable int num;
void stuInitFunc(int stuAge, int stuHight, int stuNum) const;
};
再次编译程序,不会报错。
四、返回自身对象的引用--this
this用在成员函数中是一个隐藏起来的函数参数,表示的是指向本对象的指针,那么*tthis表示改指针指向的对象也就是本对象。换句话说,*this表示调用这个成员函数的那个对象。
如下在类student的成员函数中,三种写法都是正确的:
方式一:没有this,推荐使用这种方式
void student::stuInitFunc(int stuAge, int stuHight, int stuNum) const
{
age = stuAge;
hight = stuHight;
num = stuNum;
}
方式二:指向本类对象的指针,类似于student stu; student *this = &stu;
void student::stuInitFunc(int stuAge, int stuHight, int stuNum) const
{
this->age = stuAge;
this->hight = stuHight;
this->num = stuNum;
}
方式三:表示调用这个成员函数的那个类对象
void student::stuInitFunc(int stuAge, int stuHight, int stuNum) const
{
(*this).age = stuAge;
(*this).hight = stuHight;
(*this).num = stuNum;
}
以上三种方式都是等价的,没有什么区别。
在使用this时,需要注意:
(1)this本身是一个指针常量,总是指向这个对象本身,不可以让this再指向其它地方。
(2)this指针只能在类成员函数中使用,全局函数、静态函数等都不能使用this指针。
(3)在const成员函数中,this指针是指向const对象的const指针。例如,类类型为student,那么this就是const student *const类型。
(4)当形参名与成员变量名相同时,用this.成员变量名来和形参变量区分开,如下所示:
void student::stuInitFunc(int age, int hight, int num)
{
(*this).age = age;
(*this).hight = hight;
(*this).num = num;
}
或
void student::stuInitFunc(int age, int hight, int num)
{
this->age = age;
this->hight = hight;
this->num = num;
}
(5)当一个成员函数需要返回一个类对象变量时,可以用return (*this);把这个类对象返回给类对象本身。如下所示:
student student::stuInitFunc(int age, int hight, int num)
{
this->age = age;
this->hight = hight;
this->num = num;
return (*this);//返回调用类对象本身。这里类对象为stu1
}
调用方式如下:
student stu1; student stu2; stu2 = stu1.stuInitFunc(20,180,1001); cout << stu2.age << " " << stu2.hight << " " << stu2.num << endl;
五、static成员
在C中static有两个作用,这里看看static在C++中有什么作用。
1、static修饰成员变量
在.h头文件中,静态成员变量在类中这样声明:
class student
{
public:
static int age; //声明为静态成员变量
static int hight;//声明为静态成员变量
static int num; //声明为静态成员变量
int weight; //声明为普通成员变量
void stuInitFunc(int stuAge,int stuHight,int stuNum,int stuWeight);
};
声明以后,在.cpp文件中,不要把成员变量定义在类的成员函数中,而是定义在.cpp文件的开头,这样能够保证在调用任何函数之前这个静态成员变量已经被初始化,从而保证这个静态成员变量能够被正常使用,如下定义:
int student::age = 18; int student::hight= 180; int student::num = 10002;
我们在mian.cpp文件中创建两个student类的新对象stu1和stu2,然后stu1和stu2都调用且打印age、hight和num这三个成员变量的值,再用stu1去修改static修饰的这三个成员变量,再用stu1和stu2调用这三个成员变量且打印这个成员变量的值,可以看到两个类对象的这个成员变量的值是一样的:
student stu1,stu2; //打印stu1和stu2调用成员变量的值 cout << stu1.age << " " << stu1.hight << " " << stu1.num << endl; cout << stu2.age << " " << stu2.hight << " " << stu2.num << endl; //类对象stu1修改成员变量的值 stu1.age = 20; stu1.hight = 185; stu1.num = 1005; //打印stu1和stu2调用成员变量的值 cout << stu1.age << " " << stu1.hight << " " << stu1.num << endl; cout << stu2.age << " " << stu2.hight << " " << stu2.num << endl;
2、static修饰成员函数
在声明类对象和声明成员函数时,在成员函数前面加static,如下所示:
class student
{
public:
static int age;
static int hight;
static int num;
int weight;
static void stuInitFunc(int stuAge, int stuHight, int stuNum, int stuWeight);
};
在.cpp中定义成员函数,在成员函数前面就不需要加static修饰符,static修饰的成员函数和普通成员函数的定义方式是一样的,如下:
//必须先在这里定义静态成员变量,才能在静态成员函数中再次给静态成员变量赋值
int student::age = 20;
int student::hight = 185;
int student::num = 1005;
void student::stuInitFunc(int stuAge,int stuHight,int stuNum,int stuWeight)
{
age = stuAge; //static修饰的成员变量可以在static修饰的成员变量定义或者被赋值
hight = stuHight;//static修饰的成员变量可以在static修饰的成员变量定义或者被赋值
num = stuNum; //static修饰的成员变量可以在static修饰的成员变量定义或者被赋值
//weight = stuWeight;//不是static修饰的成员变量不可以在static修饰的成员变量定义或者被赋值
cout << "student类的成员函数" << endl;
}
用staitic修饰成员函数后,可以用 ”类名::静态成员函数名” 或 “类对象.静态成员函数名” 来调用静态成员函数。如下所示:
#include#include #include #include"main.h" using namespace std; //必须先在这里定义静态成员变量,才能在静态成员函数中再次给静态成员变量赋值 int student::age = 20; int student::hight = 185; int student::num = 1005; void student::stuInitFunc(int stuAge,int stuHight,int stuNum,int stuWeight) { age = stuAge; hight = stuHight; num = stuNum; cout << "student类的成员函数" << endl; } int main() { student stu1,stu2; //打印student、stu1和stu2调用成员变量的值 cout << student::age << " " << student::hight << " " << student::num << endl; cout << stu1.age << " " << stu1.hight << " " << stu1.num << endl; cout << stu2.age << " " << stu2.hight << " " << stu2.num << endl<



