#include <iostream> #include "Node.cpp" using namespace std; int main() { std::cout << "Hello World! "; int a[5], * p; int sum, i; a[0] = 1; a[1] = 2; a[2] = 3; a[3] = 4; a[4] = 5; for ( sum = a[0],i = 1; i < 5; i++) { sum += a[i]; } sum = 0; for (sum = *a, i = 1; i < 5; i++) { sum += *(a + i); } sum = 0; for (sum = *a, p = a + 1; p < a + 5; p++) { sum += *p; } //a+1 等同于&a[1] p = new int[10]; delete[]p; Node node1("Roger", 20), node2(node1); strcpy_s(node2.name, sizeof("Wendy"), "Wendy"); node2.age = 30; cout << node1.name << ' ' << node1.age << ' ' << node2.name << ' ' << node2.age; node1 = node2; /*free(node1.name); free(node2.name);*/ }
#include <string.h> #include <corecrt_malloc.h> struct Node { char* name; int age; public: Node(char* n = " ", int a = 0) { name = _strdup(n); age = a; } ~Node() { if (name != 0) { free(name); } } Node(const Node& n) { name = _strdup(n.name); age =n.age; } Node& operator =(const Node& n) { if (this != &n) { if (name != 0) { free(name); } name = _strdup(n.name); age = n.age; } return *this; } };