[问题] constructor的问题

楼主: unknown (ya)   2014-12-16 10:13:15
开发平台(Platform): (Ex: VC++, GCC, Linux, ...)
g++
额外使用到的函数库(Library Used): (Ex: OpenGL, ...)
问题(Question):
小弟是初学者,尝试写nested class,class B为class A 的class member。其中A的const
ructor如果放在initilization list里会有预期的100 100;但放在function boy里会
100 10的错误,想了解问题在哪?
喂入的资料(Input):
预期的正确结果(Expected Output):
100 100
错误结果(Wrong Output):
100 10
程式码(Code):(请善用置底文网页, 记得排版)
1 #include<iostream>
2 using namespace std;
3 class A
4 {
5 public:
6 A(int ia)
7 {
8 B Ted(ia);
9 i=ia;
10
解问疆11 private:
13 int i;
14 class B
15 {
16 public:
17 B()
18 {
19 j=10;
20 }
21 B(int jb)
22 {
23 j=jb;
24 }
25 private:
26 int j;
27 friend int main();
28 } Ted;
29 friend int main();
30
31 };
32
33
34 int main()
35
36 {
37 A tt(100);
38 cout<<tt.i<<"\t"<<tt.Ted.j<<endl;
39 return 0;
40
41 }
42
~
补充说明(Supplement):
作者: scwg ( )   2014-12-16 10:45:00
写 A(){ B Ted(ia); } 是宣告区域变量 Ted, 没有动到 classmember 的那个 Ted

Links booklink

Contact Us: admin [ a t ] ucptt.com