#ifndef TEST_H_ #define TEST_H_ #define BUF_SIZE_ 100 class testClass { public: //construct testClass(char*); testClass(testClass&); //destruct ~testClass(); //operator testClass& operator = (const testClass&); testClass& operator == (const testClass&); //getter char* get_name(void); char* get_value(void); //setter void set_value(char*); private: char* _name; char* _cp_value; char* _pt_value; }; //construct testClass::testClass(char* value) { //name this->_name = new char[BUF_SIZE_]; strcpy(this->_name, value); //cp_value this->_cp_value = new char[BUF_SIZE_]; strcpy(this->_cp_value, value); // *this->_cp_value = '\0'; //pt_value this->_pt_value = new char[BUF_SIZE_]; strcpy(this->_pt_value, value); //message out printf("construct: %s\n", this->_name); } //copy construct testClass::testClass(testClass& src_obj) { //コピー //PHPではオブジェクト型以外はコピーされる //name this->_name = new char[BUF_SIZE_]; strcpy(this->_name, src_obj._name); //cp_value this->_cp_value = new char[BUF_SIZE_]; strcpy(this->_cp_value, src_obj._cp_value); //アドレスを参照する //PHPではオブジェクト型は参照される //pt_value this->_pt_value = src_obj._pt_value; //message out printf("copy construct: %s\n", this->_name); } //destruct testClass::~testClass() { //message out printf("destruct: %s [%s:%s]\n", this->_name, this->_cp_value, this->_pt_value); delete [] this->_name; delete [] this->_cp_value; delete [] this->_pt_value; } //operator testClass& testClass::operator = (const testClass& src_obj) { //コピーする演算子として作成 strcpy(this->_name, src_obj._name); strcpy(this->_cp_value, src_obj._cp_value); strcpy(this->_pt_value, src_obj._pt_value); return *this; } testClass& testClass::operator == (const testClass& src_obj) { //アドレスを参照する演算子として作成 //name if (this->_name != NULL) delete [] this->_name; this->_name = src_obj._name; //cp_value if (this->_cp_value != NULL) delete [] this->_cp_value; this->_cp_value = src_obj._cp_value; //pt_value if (this->_pt_value != NULL) delete [] this->_pt_value; this->_pt_value = src_obj._pt_value; return *this; } //getter char* testClass::get_name(void) { return this->_name; } char* testClass::get_value(void) { //copy:point書式で返す char* result = new char [BUF_SIZE_ * 3]; sprintf(result, "%s:%s", this->_cp_value, this->_pt_value); return result; } //setter void testClass::set_value(char* value) { printf("set before =====> %s <= %s\n", this->get_value(), value); strcpy(this->_cp_value, value); strcpy(this->_pt_value, value); printf("set after =====> %s\n", this->get_value()); } #endif /*TEST_H_*/