メタ関数<
1、コンストラクション関数の使用
2、構築と付与文
3、構造関数の実行順序
≪実行順序|Execute Order|emdw≫:まず、グループ化されたオブジェクトのコンストラクション関数を実行します.複数ある場合は、初期化リストの順序ではなく定義された順序で実行します.構造関数の呼び出し順序が逆である.
初期化リストはconst属性に値を割り当てるために使用されます.
コードは次のとおりです.
4、newとdelete
Newはクラスの構造関数を実行することができ、deleteはクラスの構造関数を実行することができる.
malloc free new deleteは互換的に使用できますが、newは先に空間を開きます-->呼び出し構造;delete--->先析构造、解放空间;
5、static
静的メンバーはこのクラスのバイトのサイズを占めません.このインスタンスに属していないのは、グローバル変数に相当します.
(1)、コードは以下の通りである.
int main(void){
//Test t1 = 10;//
// ;
Test t2 = Test(1, 2); // ---> ;
// Test , t2 ; ( t2);
}
2、構築と付与文
Test g(){
// , ( );
}
int main(void){
Test t1(1, 2);
Test t2;
Test t3 = t1; // t1 t3, ;
Test t3(t1);
t2 = t1; // t1 t2 , ;
Test m = g(); // , m, C++ m;
m = g();// , , ,
}
// , , , C++
3、構造関数の実行順序
≪実行順序|Execute Order|emdw≫:まず、グループ化されたオブジェクトのコンストラクション関数を実行します.複数ある場合は、初期化リストの順序ではなく定義された順序で実行します.構造関数の呼び出し順序が逆である.
初期化リストはconst属性に値を割り当てるために使用されます.
コードは次のとおりです.
#include
using namespace std;
class A{
public:
A(int a){
m = a;
}
private:
int m;
};
// , ; ,
// ;
class B{
public: // : , B , A ,
// , A , ;
B(int q, int a, int b, int c) : a1(b), a2(c), c(0){
x = q;
y = a;
}
// : , , , ;
// ;
// :a1、a2、b
// const ;
private:
int x;
int y;
A a1;
A a2;
const int c; // c ( )
};
int main(void){
B b(1, 2, 3, 4);
}
4、newとdelete
Newはクラスの構造関数を実行することができ、deleteはクラスの構造関数を実行することができる.
malloc free new deleteは互換的に使用できますが、newは先に空間を開きます-->呼び出し構造;delete--->先析构造、解放空间;
5、static
静的メンバーはこのクラスのバイトのサイズを占めません.このインスタンスに属していないのは、グローバル変数に相当します.
(1)、コードは以下の通りである.
#include
using namespace std;
class A{
public:
A(){}
int getD(){
return d;
}
void Add(){
d++;
}
static void getC(){ // ;
// 、 ;
cout<
(2)、 :
6、 ( = )
i>、 ;
ii>、 ; ;
iii>、 ;
7、
, , ; , ;
class M{};// , ,1 ;
8、
:
: , ;
9、
<
(1)、 :
#include
using namespace std;
// : ;
template
class Complex; //
template
ostream& operator< &c);//
template
class Complex{
public:
friend ostream& operator<<(ostream &out, const Complex &c); // <
; ;
Complex(Type a, Type b);
Complex(const Complex &c){}
Complex& operator=(const Complex &c){}
~Complex(){}
public:
void printComplex();
Complex operator+(const Complex &c);
private:
Type a;
Type b;
};
// +
template
Complex::Complex(Type a, Type b){
this->a = a;
this->b = b;
}
template
void Complex::printComplex(){
cout<
Complex Complex::operator+(const Complex &c){
return Complex(a+c.a, b+c.b);
}
// <
ostream& operator< &c){
out< c1(1, 2);
Complex c2(3, 4);
Complex c3 = c1 + c2;
cout<
(2)、