Skip to content

Commit

Permalink
增加复制构造函数和赋值运算符重载,针对类中有new分配的情况
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee committed May 8, 2018
1 parent 91b4df3 commit fbe8fcf
Show file tree
Hide file tree
Showing 10 changed files with 456 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ chapter_8/a.out
chapter_8/carinfo.txt
chapter_9/a.out
chapter_10/a.out
chapter_12/a.out
80 changes: 80 additions & 0 deletions chapter_12/.vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{
"configurations": [
{
"name": "Mac",
"includePath": [
"/usr/include",
"/usr/local/include",
"${workspaceRoot}"
],
"defines": [],
"intelliSenseMode": "clang-x64",
"browse": {
"path": [
"/usr/include",
"/usr/local/include",
"${workspaceRoot}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
},
"macFrameworkPath": [
"/System/Library/Frameworks",
"/Library/Frameworks"
]
},
{
"name": "Linux",
"includePath": [
"/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../include/c++/4.8.5",
"/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../include/c++/4.8.5/x86_64-redhat-linux",
"/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../include/c++/4.8.5/backward",
"/usr/lib/gcc/x86_64-redhat-linux/4.8.5/include",
"/usr/local/include",
"/usr/include",
"${workspaceRoot}",
"${workspaceRoot}/header"
],
"defines": [],
"intelliSenseMode": "clang-x64",
"browse": {
"path": [
"/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../include/c++/4.8.5",
"/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../include/c++/4.8.5/x86_64-redhat-linux",
"/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../include/c++/4.8.5/backward",
"/usr/lib/gcc/x86_64-redhat-linux/4.8.5/include",
"/usr/local/include",
"/usr/include",
"${workspaceRoot}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
},
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++14"
},
{
"name": "Win32",
"includePath": [
"C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include",
"${workspaceRoot}"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"intelliSenseMode": "msvc-x64",
"browse": {
"path": [
"C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include/*",
"${workspaceRoot}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
}
],
"version": 3
}
31 changes: 31 additions & 0 deletions chapter_12/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
// https://github.com/Microsoft/vscode-cpptools/blob/master/launch.md
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
//"internalConsoleOptions": "neverOpen",
"preLaunchTask": "main",
"additionalSOLibSearchPath": ""
}
]
}
32 changes: 32 additions & 0 deletions chapter_12/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"files.associations": {
"iostream": "cpp",
"ostream": "cpp",
"cmath": "cpp",
"climits": "cpp",
"cfloat": "cpp",
"array": "cpp",
"istream": "cpp",
"typeinfo": "cpp",
"string": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"exception": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"limits": "cpp",
"new": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"type_traits": "cpp"
},
"C_Cpp.errorSquiggles": "Disabled"
}
25 changes: 25 additions & 0 deletions chapter_12/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "main",
"type": "shell",
"command": "g++",
"args": [
"-g",
"-std=c++11",
"-Wall",
"main.cpp",
"file.cpp",
"debug.cpp"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": []
}
]
}
86 changes: 86 additions & 0 deletions chapter_12/debug.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <stdarg.h>
#include "header/debug.h"

#define ANSI_COLOR_RED "\x1b[1;31m"
#define ANSI_COLOR_GREEN "\x1b[1;32m"
#define ANSI_COLOR_YELLOW "\x1b[1;33m"
#define ANSI_COLOR_PURPLE "\x1b[1;35m"
#define ANSI_COLOR_CYAN "\x1b[29m"
#define ANSI_COLOR_CYAN_LESS "\x1b[29m"
#define ANSI_COLOR_RESET "\x1b[0m"

#define PRINT_DEBUG "DEBUG"
#define PRINT_INFO "INFO"
#define PRINT_WARN "WARN"
#define PRINT_ERROR "ERROR"

#define PRINT_LEN 10240

static void _common_printf(FILE * fp, const char * time_cl, const char * type_cl,
const char * func_cl, const char * content_cl,
const char * level, const char * file,
const int line, const char * func, const char * fmt)
{
struct timeval tv;
gettimeofday(&tv, NULL);
char datestr[20];
struct tm tm;
time_t timesec = tv.tv_sec;
localtime_r(&timesec, &tm);
strftime(datestr, sizeof(datestr), "%Y-%m-%d %H:%M:%S", &tm);
/* fprintf(fp, "%s" "%s.%06ld " ANSI_COLOR_RESET
"%s" "[%-05s]" ANSI_COLOR_RESET
"%s" "[%s, %4d][%-10s] " ANSI_COLOR_RESET
"%s" "%s" ANSI_COLOR_RESET,
time_cl, datestr, tv.tv_usec, type_cl,
level, func_cl, file, line, func, content_cl, fmt); */
fprintf(fp, "%s" "[%-05s]" ANSI_COLOR_RESET
"%s" "[%s,%d] " ANSI_COLOR_RESET
"%s" "%s" ANSI_COLOR_RESET,
type_cl,level, func_cl, file, line, content_cl, fmt);
fflush(fp);
}

int common_printf(FILE * fp, const int level, const char * file,
const int line, const char * func, const char * fmt, ...)
{
int i;
char buf[PRINT_LEN];

va_list args;
va_start(args, fmt);
i = vsnprintf(buf, PRINT_LEN, fmt, args);
va_end(args);

switch (level) {
case LEVEL_DEBUG:
_common_printf(fp, ANSI_COLOR_CYAN_LESS, ANSI_COLOR_GREEN,
ANSI_COLOR_CYAN, ANSI_COLOR_GREEN,
PRINT_DEBUG, file, line, func, buf);
break;

case LEVEL_INFO:
_common_printf(fp, ANSI_COLOR_CYAN_LESS, ANSI_COLOR_YELLOW,
ANSI_COLOR_CYAN, ANSI_COLOR_YELLOW,
PRINT_INFO, file, line, func, buf);
break;

case LEVEL_WARN:
_common_printf(fp, ANSI_COLOR_CYAN_LESS, ANSI_COLOR_PURPLE,
ANSI_COLOR_CYAN, ANSI_COLOR_PURPLE,
PRINT_WARN, file, line, func, buf);
break;
case LEVEL_ERROR:
_common_printf(fp, ANSI_COLOR_CYAN_LESS, ANSI_COLOR_RED,
ANSI_COLOR_CYAN, ANSI_COLOR_RED,
PRINT_ERROR, file, line, func, buf);
break;
default:
break;
}

return i;
}
73 changes: 73 additions & 0 deletions chapter_12/file.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <iostream>
#include <cstring>
#include "header/file.h"

using namespace std;


int StringMe::num_strings = 0;

StringMe::StringMe(const char *s)
{
show("StringMe::StringMe(const char *s)\n");
len = std::strlen(s);
str = new char[len + 1];
std::strcpy(str, s);
num_strings ++;
}

StringMe::StringMe(const StringMe & st)
{
show("StringMe::StringMe(const StringMe &)\n");
num_strings++;
len = st.len;
str = new char[len + 1];
std::strcpy(str, st.str);
}

StringMe::StringMe()
{
show("StringMe::StringMe()\n");
len = 5;
str = new char[len + 1];
std::strcpy(str, "empty");
num_strings++;
}

StringMe::~StringMe()
{
show("~StringMe::StringMe()\n");
print();
delete [] str;
num_strings--;
}

std::ostream & operator<<(std::ostream & os,
const StringMe & st)
{
os << st.str;
return os;
}

void StringMe::print(void)
{
cout << "str: " << str << endl;
cout << "num_strings: " << num_strings << endl;
}

StringMe & StringMe::operator=(const StringMe & st)
{
show("StringMe & StringMe::operator=(const StringMe & st)\n");

if (this == &st) {
return *this;
}

delete [] str;
len = st.len;
str = new char [len + 1];
std::strcpy(str, st.str);

return *this;
}

46 changes: 46 additions & 0 deletions chapter_12/header/debug.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#ifndef __DEBUG_H__
#define __DEBUG_H__

#include <stdio.h>

extern int common_printf(FILE * fp, const int level, const char * file,
const int line, const char * func, const char * fmt, ...);

/* 打印等级 0-4 */
#define PRINT_LEVEL 4

/* 提供4个打印接口 DEBUG, INFO, WARN, ERROR */
#define LEVEL_DEBUG 4
#define LEVEL_INFO 3
#define LEVEL_WARN 2
#define LEVEL_ERROR 1

#if (PRINT_LEVEL >= LEVEL_DEBUG)
#define DEBUG(fmt, ...) \
common_printf(stdout, LEVEL_DEBUG, __FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__);
#else
#define DEBUG(fmt, ...)
#endif

#if (PRINT_LEVEL >= LEVEL_INFO)
#define INFO(fmt, ...) \
common_printf(stdout, LEVEL_INFO, __FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__);
#else
#define INFO(fmt, ...)
#endif

#if (PRINT_LEVEL >= LEVEL_WARN)
#define WARN(fmt, ...) \
common_printf(stdout, LEVEL_WARN, __FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__);
#else
#define WARN(fmt, ...)
#endif

#if (PRINT_LEVEL >= LEVEL_ERROR)
#define ERROR(fmt, ...) \
common_printf(stdout, LEVEL_ERROR, __FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__);
#else
#define ERROR(fmt, ...)
#endif

#endif
Loading

0 comments on commit fbe8fcf

Please sign in to comment.