⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 errormessage.cpp

📁 一本语言类编程书籍
💻 CPP
字号:
// ErrorMessage.cpp ErrorMessage class implementation
#include <cstring>
#include "ErrorMessage.h"
using std::cout;
using std::endl;

// Constructor
ErrorMessage::ErrorMessage(const char* pText) {
  pMessage = new char[ strlen(pText) + 1 ];             // Get space for message
  std::strcpy(pMessage, pText);                         // Copy to new memory
}

// Destructor to free memory allocated by new
ErrorMessage::~ErrorMessage() {
  cout << endl << "Destructor called." << endl;
  delete[] pMessage;                                    // Free memory for message
}

// Change the message
void ErrorMessage::resetMessage() {
  // Replace message text with asterisks
  for(char* temp = pMessage ; *temp != '\0' ; *(temp++) = '*')
    ;
}

// Assignment operator
ErrorMessage& ErrorMessage::operator=(const ErrorMessage& message) {
  if(this == &message)                              // Compare addresses, if equal
    return *this;                                   // return left operand

  delete[] pMessage;                                // Release memory for left operand
  pMessage = new char[ strlen(message.pMessage) + 1];

  // Copy right operand string to left operand
  std::strcpy(this->pMessage, message.pMessage);

  return *this;                                     // Return left operand
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -