【C++過載new】過載new以及namespace使用
阿新 • • 發佈:2018-11-08
下面是過載Class內部以及全域性的new程式碼。
class fileClass{ public: int a; int b; fileClass(){ a = 0; b = 0; } fileClass(int aa,int bb):a(aa),b(bb){} void* operator new(size_t size) { cout << "new : " << size << endl; return malloc(size); } }; static void* operator new(size_t size) { cout << "new all : " << size << endl; return malloc(size); }
namespace使用
namespace TV
{
int a = 100;
double b = 111;
void fun(){cout << "namespace TV." << endl;}
}
using namespace TV;
fun();
c++ 測試程式碼
#include <iostream> #include <fstream> #include <string> using namespace std; class Point { public: int x,y; Point(int xx, int yy):x(xx),y(yy){} virtual void display() const {cout << "[" << x << "," << y << "]" <<endl;} friend ostream &operator<< (ostream &output,const Point &p) { output << "[" << p.x << "," << p.y << "]" <<endl; return output; } Point operator++(int) { Point temp = *this; (this->x)++; (this->y)++; return temp; } friend Point operator*(const Point &p1,const Point &p2) { return Point(p1.x*p2.x,p1.y*p2.y); } private: virtual void display1() const {cout << "[" << x << "," << y << "]" <<endl;} }; class PointV : public Point { public: int V; PointV(int xx,int yy,int vv = 0):Point(xx,yy) { V = vv; } virtual void display() const {cout << "[" << x << "," << y << "," << V << "]" <<endl;} virtual void display1() const {cout << "[" << x << "," << y << "," << V << "]" <<endl;} }; class fileClass{ public: int a; int b; fileClass(){ a = 0; b = 0; } fileClass(int aa,int bb):a(aa),b(bb){} void* operator new(size_t size) { cout << "new : " << size << endl; return malloc(size); } }; static void* operator new(size_t size) { cout << "new all : " << size << endl; return malloc(size); } namespace TV { int a = 100; double b = 111; void fun(){cout << "namespace TV." << endl;} } void matAB(double **A, double **B, double **C, int *sizeAB) { int m = sizeAB[0], n = sizeAB[1], p = sizeAB[2]; for (int i = 0;i < m;i++) { for (int j = 0;j < p;j++) { for (int k = 0;k < n;k++) { C[i][j] += A[i][k]*B[k][j]; } } } } int main4() { Point myP(1,1), *pPoint = NULL; PointV myV(11,22); myV++; pPoint = &myP; pPoint = &myV; pPoint->display(); myP = myV; cout << myV << endl; myP.display(); myP++; myP = myP*myP; cout << myP << endl; ofstream outfile; outfile.open("xiao.data",ios::binary); if (!outfile) { cerr << "eeee"; } outfile << "xiaogongwei123" << endl; outfile << 12122 << endl; outfile.close(); //char linstr[10] = {0}; string linstr; ifstream infile("xiao.data",ios::in|ios::_Nocreate); infile >> linstr; cout << linstr << endl; infile >> linstr; cout << linstr << endl; infile.close(); using namespace TV; fun(); /*char *p = "123456"; cout << *++p << endl;*/ fileClass *pfileC = new fileClass; int m = 4, n = 3, p = 2; double **A = (double **)malloc(m*sizeof(double *)),**B = (double **)malloc(n*sizeof(double *)), **C = (double **)malloc(m*sizeof(double *)); int sizeAB[3] = {4, 3, 2}; for (int i = 0;i < m;i++) { A[i] = (double *)malloc(n*sizeof(double)); } for (int i = 0;i < n;i++) { B[i] = (double *)malloc(n*sizeof(double)); } for (int i = 0;i < m;i++) { C[i] = (double *)malloc( p * sizeof(double) ); } for (int i = 0;i < m;i++) { for (int j = 0;j < n;j++) { A[i][j] = 1; } } for (int i = 0;i < n;i++) { for (int j = 0;j < p;j++) { B[i][j] = 1; } } for (int i = 0;i < m;i++) { for (int j = 0;j < p;j++) { C[i][j] = 0; } } matAB(A, B, C, sizeAB); for (int i = 0;i < m;i++) { for (int j = 0;j < p;j++) { cout << C[i][j] << ","; } cout << endl; } system("pause"); return 0; }