本文共 1386 字,大约阅读时间需要 4 分钟。
#include <iostream>
#include <string.h>#include <stdio.h>#include <stdlib.h>using namespace std;/**/
class Student{string m_name;
const int m_id; //A1int m_score;public:int show_info_no_const(void) { cout <<"no const function: name "<< m_name << ", id " << m_id<<endl;return 0;}int show_info(void) const { cout <<"name "<< m_name << ", id " << m_id<<endl;// m_score=90; //A2 此处会有编译错误,原因就是show_info 被const来修饰了。return 0;}void set_name(char new_name);~Student();Student():m_id(999){};Student(const char h);Student(const Student &obj); };Student::Student(const Student &obj):m_id(888)//A1{ cout <<"call copy constructor "<<endl;m_name = obj.m_name;}#if 1Student::Student(const char* h):m_id(777){ m_name=h;}
#endif#if 1
Student::~Student(){ }void Student::set_name(char* new_name)
{ m_name=new_name;//m_id=9;//A1 m_id是不可以再被修改的。}
#endif//测试A3.1void show_student(const Student& Stu) //A3{ Stu.show_info();// Stu.show_info_no_const(); //A3.1 此函数实际上没有改变Stu的值 ,但是由于它没有使用const进行修饰,所以发生了const属性丢失的情况。}int main(){ Student A1;A1.set_name("A1");A1.show_info();Student A2("abl");A2.show_info();Student A3(A2);A3.show_info();return 0;
}
转载于:https://blog.51cto.com/yaxinsn/2394690