C++局部类
C++局部类的最主要特点是能对外隐藏实现,与java的嵌套类是一个概念。由于不能访问外部类的成员,能力还是比较弱的。 局部类(Local Classes) 在函数内部定义的类为局部类,专为函数内部使用,以实现某种功能,一般来说可以用外部类代替,但局部类也有一些自身的特征 局部类不能定义static成员变量,也不能访问函数内部类 ...
It’s not what you know, it’s how you think
C++局部类的最主要特点是能对外隐藏实现,与java的嵌套类是一个概念。由于不能访问外部类的成员,能力还是比较弱的。 局部类(Local Classes) 在函数内部定义的类为局部类,专为函数内部使用,以实现某种功能,一般来说可以用外部类代替,但局部类也有一些自身的特征 局部类不能定义static成员变量,也不能访问函数内部类 ...
对每个N,使得1,2,3..N^2,排列成N×N矩阵,并且横、竖、两条对角线的和都相等。 #include <vector>#include <algorithm>#include <iostream>#include <iomanip>using namespace std; typedef vector< vector<int> > Matrix; inline void Print( const Matrix& array, int size ) { for (int i=1; i<=size; ++i ) { for (int j=1; j<=size; ++j ) cout << setw(5) << array[i][j]; cout << endl; } } inline void Meshgrid( Matrix& A, Matrix& B, int size ) { for (int i=1; i<=size; ++i ) for (int j=1; j<=size; ++j ) A[i][j] = j, B[i][j] = i; } void Magic( Matrix& mat, int size ); int main() { int N; cout << "Input a Number:\n"; cin >> N; if ( N > 1000 ) { cout << "The Number Inputed is Too Large!\n"; return 1; } Matrix Array(N+1); for ...
C++支持成员的指针储存,并且之后可以使用->*, .* 来调用。 #include <iostream>using namespace std; classTest { public: Test(int n=0) : aa(n) {} Test( const Test & M ): aa(M.aa) {} ~Test() {} public: void Set( int n ) { aa = n; } void Print() { cout << aa << endl; } static void Yeah() { cout << "Yeah" << endl; } private: int aa; }; // typedef函数指针 typedef void (Test::*FuncPtrType)(); // 直接赋值 void (Test::*ptrSet)(int) = &Test::Set; // 静态函数 void (*PStaticYeah)() = &Test::Yeah; int main() { // 静态函数不用与对象关联,所以在对象生成以前就能用 PStaticYeah(); Test * p = new Test(5); // ->* (p ...