回答編集履歴
2
内容追記
test
CHANGED
@@ -1,6 +1,12 @@
|
|
1
1
|
ポインタ変数の宣言のみで実体がないからです。
|
2
2
|
|
3
|
+
```C++
|
4
|
+
|
3
|
-
|
5
|
+
pp = new Printer();
|
6
|
+
|
7
|
+
```
|
8
|
+
|
9
|
+
のように実体を生成する必要があります。
|
4
10
|
|
5
11
|
|
6
12
|
|
@@ -64,7 +70,9 @@
|
|
64
70
|
|
65
71
|
Wnd w;
|
66
72
|
|
73
|
+
// "p"
|
67
74
|
|
75
|
+
// "obj"
|
68
76
|
|
69
77
|
return 0;
|
70
78
|
|
1
内容追記
test
CHANGED
@@ -1,3 +1,73 @@
|
|
1
1
|
ポインタ変数の宣言のみで実体がないからです。
|
2
2
|
|
3
|
-
```pp = new Printer();```のように実体を生成する必要があります。
|
3
|
+
``` pp = new Printer();``` のように実体を生成する必要があります。
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
objのほうはChironianさんも指摘のとおり、動作はすると思います。
|
8
|
+
|
9
|
+
以下の類似ソースでは正常に動作します。
|
10
|
+
|
11
|
+
```C++
|
12
|
+
|
13
|
+
#include <iostream>
|
14
|
+
|
15
|
+
using namespace std;
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
class Printer;
|
20
|
+
|
21
|
+
Printer *obj = NULL;
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
class Printer{
|
26
|
+
|
27
|
+
public:
|
28
|
+
|
29
|
+
Printer(){
|
30
|
+
|
31
|
+
obj = this;
|
32
|
+
|
33
|
+
}
|
34
|
+
|
35
|
+
void Print( string s){
|
36
|
+
|
37
|
+
cout << s << endl;
|
38
|
+
|
39
|
+
}
|
40
|
+
|
41
|
+
};
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
class Wnd{
|
46
|
+
|
47
|
+
public:
|
48
|
+
|
49
|
+
Wnd(){
|
50
|
+
|
51
|
+
Printer p;
|
52
|
+
|
53
|
+
p.Print("p");
|
54
|
+
|
55
|
+
obj->Print("obj");
|
56
|
+
|
57
|
+
}
|
58
|
+
|
59
|
+
};
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
int main() {
|
64
|
+
|
65
|
+
Wnd w;
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
return 0;
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
```
|