Created
March 3, 2019 06:15
-
-
Save frankie-yanfeng/8af00c7bf9c88d5d6420f81eed087273 to your computer and use it in GitHub Desktop.
C++ constructor array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
class CSample { | |
int x; | |
public: | |
CSample() { | |
cout << "Constructor 1 Called" << endl; | |
} | |
CSample(int n) { | |
x = n; | |
cout << "Constructor 2 Called" << endl; | |
} | |
}; | |
class Test { | |
public: | |
Test( int n) { } //(1) | |
Test( int n, int m) { } //(2) | |
Test() { } //(3) | |
}; | |
int main(){ | |
CSample array1[2]; | |
cout << "step1"<<endl; | |
CSample array2[2] = {4,5}; | |
cout << "step2"<<endl; | |
CSample array3[2] = {3}; | |
cout << "step3"<<endl; | |
CSample * array4 = | |
new CSample[2]; | |
delete []array4; | |
Test arrayA[3] = { 1, Test(1,2) }; | |
// 三个元素分别用(1),(2),(3)初始化 | |
Test arrayB[3] = { Test(2,3), Test(1,2) , 1}; | |
// 三个元素分别用(2),(2),(1)初始化 | |
Test * pArray[3] = { new Test(4), new Test(1,2) }; | |
//两个元素分别用(1),(2) 初始化 | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment