-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
43 lines (33 loc) · 802 Bytes
/
test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
Test declaration of class pointer variable with unknown size.
There is a conflict here!
*/
#include <iostream>
using namespace std;
class MyClass {
public:
int N;
int * myptr; // incomplete type (?)
MyClass(int N);
void myfunction();
};
MyClass::MyClass(int N1) {
cout << "In constructor\n";
N = N1;
// assign the pointer variable now that its size is known
int *myptr = new int[N]; // but type is still incomplete
for (int i=0;i<N;i++) {
myptr[i] = i;
cout << myptr[i] << "\n"; }
}
void MyClass::myfunction() {
cout << "In myfunction\n";
// but myptr has fallen out of scope
for (int i=0;i<N;i++) {
cout << myptr[i] << "\n"; }
}
int main () {
MyClass myclass1(5);
myclass1.myfunction();
return 1.;
}