// naive:
int arr[10];
memset(arr, 0, sizeof(a));
// modern:
// #include <array>
std::array<int, 10> arr;
arr.fill(0);
// naive:
int *arr = new int[10];
memset(arr, 0, 10 * sizeof(int));
// modern:
// #include <vector>
std::vector<int> arr(10);
// naive:
char str[] = "Hello, C++!";
// modern:
// #include <string>
std::string str = "Hello, C++!";
// or auto str = "Hello, C++!"s;
// naive:
int ival;
int *p = &ival;
// modern:
int ival;
int &rval = ival;
// naive:
MyClass *obj = new MyClass;
obj->someMethod(args);
// modern:
auto obj = std::make_unique<MyClass>();
// or auto obj = std::make_shared<MyClass>();
obj->someMethod(args);
// naive:
typedef int (*func_t)(int, int);
func_t func = some_func;
// modern:
// #include <function>
std::function<int(int, int)> func = some_func;
// naive:
struct func_t
{
int operator() (int arg1, int arg2)
{
// statements
}
};
func_t func;
// modern:
// #include <function>
std::function<int(int, int)> func
= [](int arg1, int arg2)
{
// statements
};
// naive:
#define PI 3.14
// modern:
const double PI = 3.14;
// naive:
#define uint unsigned int
// modern:
typedef unsigned int uint;
// or using uint = unsigned int;
// naive:
#define max(a, b) ((a)>(b)?(a):(b))
// modern:
template<T>
inline T max(T a, T b)
{
return a>b? a: b;
}
// naive:
int ival;
double dval = (double)ival;
char *pc1 = ...;
const char *cpc = (char *)pc1;
char *pc2 = (const char *)cpc;
Derived *pd1 = ...;
Base *pb = (Base *)pd1;
Derived *pd2 = (Derived *)pb;
void *pv1 = ...;
long lval = (long)pv1;
void *pv2 = (void *)lval;
// modern:
int ival;
double dval = static_cast<double>(ival);
char *pc1 = ...;
const char *cpc = const_cast<const char *>(pc1);
char *pc2 = const_cast<char *>(cpc);
Derived *pd1 = ...;
Base *pb = dynamic_cast<Base *>(pd1);
Derived *pd2 = dynamic_cast<Derived *>(pb);
void *pv1 = ...;
long lval = reinpreter_cast<long>(pv1);
void *pv2 = reinpreter_cast<void *>(lval);
// naive:
// #include <pthread.h>
pthread_t tid;
pthread_create(&tid, func, arg);
// modern:
// #include <thread>
std::thread thr(func, arg);
未完待续…