const数据成员
const数据成员的初始化方式:
- 使用类内值(C++11支持)
- 使用构造函数的初始化列表
(如果同时使用这两种方式,以初始化列表中的值为最终初始化结果)
注意: 不能在构造函数或其他成员函数内,对const成员赋值!
代码:
Human.h
#pragma once
#include <string>
using namespace std;
extern int HumanCount;
class Human {
public:
	Human();
	Human(int age, int salary, string bldType);
	int getAge();
	Human* compare1(Human* other);
	static int gethumanCount();
	void description();
private:
	string name = "Unknown";
	int age = 28;
	int salary;
	char* addr;
	const string bloodType;
	//const string bloodType = "A";//仅C++11支持类内初始化
	
	static int humanCount;
};Human.cpp
#include "Human.h"
#include <iostream>
using namespace std;
int HumanCount = 0;
int Human::humanCount = 0;
//初始化列表
Human::Human():bloodType("未知") {
	name = "无名氏";
	age = 18;
	salary = 30000;
	HumanCount++;
	humanCount++;
}
Human::Human(int age, int salary, string bldType):bloodType(bldType) {
	cout << "调用自定义的构造函数" << endl;
	this->age = age;      //this是一个特殊的指针,指向这个对象本身
	this->salary = salary;
	name = "无名";
	addr = new char[64];
	strcpy_s(addr, 64, "China");
	HumanCount++;
	humanCount++;
}
int Human::getAge() {
	return age;
}
int Human::gethumanCount() {
	return humanCount;
}
void Human::description() {
	cout << "age:" << age << " salary:" << salary << " bloodType:" << bloodType << endl;
}
Human* Human::compare1(Human* other) {
	if (age > other->age) {
		return  this; //没有创建新的对象
	}
	else {
		return other;
	}
}
main.cpp
#include <iostream>
#include <Windows.h>
#include <string.h>
#include "Human.h"
using namespace std;
void test0() {
	Human temp1;
	Human temp2;
}
int main(void) {
	Human h1;
	Human h2(18, 8000, "B");
	h1.description();
	h2.description();
	system("pause");
	return 0;
}使用结果如下

使用步骤如下:
在类定义中定义一个const常量,其中C++11版本可以满足类内初始化的需求,为了兼容更多版本,我们可以使用其他初始化方法。
在手动定义的默认构造函数中,在初始化列表中对这个常量进行初始化,如下
Human::Human():bloodType("未知") {同时也要在自定义的构造函数中,在初始化列表中对常量进行初始化
Human::Human(int age, int salary, string bldType):bloodType(bldType) {const成员函数
需求分析:
const的Human对象,不能调用普通的成员函数。
分析:
C++认为,const(常量)对象,如果允许去调用普通的成员函数,而这个成员函数内部可能会修改这个对象的数据成员!而这讲导致const对象不再是const对象!
解决方案:
如果一个成员函数内部,不会修改任何数据成员,就把它定义为const成员函数。
代码如下
Human.h
#pragma once
#include <string>
using namespace std;
extern int HumanCount;
class Human {
public:
	Human();
	Human(int age, int salary, string bldType);
	int getAge();
	Human* compare1(Human* other);
	static int gethumanCount();
	void description() const;
private:
	string name = "Unknown";
	int age = 28;
	int salary;
	char* addr;
	const string bloodType;
	//const string bloodType = "A";//仅C++11支持类内初始化
	
	static int humanCount;
};Human.cpp
#include "Human.h"
#include <iostream>
using namespace std;
int HumanCount = 0;
int Human::humanCount = 0;
//初始化列表
Human::Human():bloodType("未知") {
	name = "无名氏";
	age = 18;
	salary = 30000;
	HumanCount++;
	humanCount++;
}
Human::Human(int age, int salary, string bldType):bloodType(bldType) {
	cout << "调用自定义的构造函数" << endl;
	this->age = age;      //this是一个特殊的指针,指向这个对象本身
	this->salary = salary;
	name = "无名";
	addr = new char[64];
	strcpy_s(addr, 64, "China");
	HumanCount++;
	humanCount++;
}
int Human::getAge() {
	return age;
}
int Human::gethumanCount() {
	return humanCount;
}
void Human::description() const {
	cout << "age:" << age << " salary:" << salary << " bloodType:" << bloodType << endl;
}
Human* Human::compare1(Human* other) {
	if (age > other->age) {
		return  this; //没有创建新的对象
	}
	else {
		return other;
	}
}
main.cpp
#include <iostream>
#include <Windows.h>
#include <string.h>
#include "Human.h"
using namespace std;
void test0() {
	Human temp1;
	Human temp2;
}
int main(void) {
	const Human h1;
	Human h2(18, 8000, "B");
	h1.description();
	h2.description();
	system("pause");
	return 0;
}


















