鍏堣創孌墊祴璇曚唬鐮侊細
1 #include<iostream>
2 using namespace std;
3 class A
4 {
5 int mem1,mem2;
6 public:
7 A(const A& a);
8 A(int a, int b):mem1(a),mem2(b){}
9 int fun1(){return mem1;}
10 int fun2(){return mem2;}
11 };
12 A::A(const A& a)
13 {
14 mem1 = a.mem1;
15 mem2 = a.mem2;
16 cout << "A's copy constructor called"<<endl;
17 }
18 class B : public A
19 {
20 int mem3;
21 public:
22 B(int a, int b, int c): A(a,b),mem3(c){}
23 int fun3(){return fun1()};
24 };
25 int main()
26 {
27 B b(1,2,3);
28 A aob(b);
29 }
30
榪欐浠g爜杈撳嚭錛?span style="font-family: 'lucida Grande', Verdana; font-size: 14px; line-height: 23px; background-color: #ffffff; ">A's copy constructor called錛堣繖鏄敤G++緙栬瘧鍣ㄧ殑錛孌EV C++ 緙栬瘧閫氳繃錛屽彲鏄繍琛屾病鏈夎緭鍑猴級
紜疄濡備功涓婅鐨勬淳鐢熺被瀵硅薄鍙互璧嬪肩粰鍩虹被鐨勫璞$殑寮曠敤錛屾墍浠ヨ皟鐢ㄤ簡鎷瘋礉鏋勯犲嚱鏁般傚叾瀹炴牴鎹奿nside the C++ object model銆嬬殑璇存硶錛屾淳鐢熺被鐨勫璞′腑灝嗕細淇濆瓨鍩虹被鐨刵on-static鏁版嵁鎴愬憳鐨勶紝閭d箞鍗充嬌涓嶅彲瑙侊紝鍙互鐢ㄦ潵鍒濆鍖栦篃鍦ㄦ儏鐞嗕箣涓?br />鍙槸鍐嶇湅琚垵濮嬪寲瀵硅薄璋冪敤鍏舵垚鍛樺嚱鏁扮殑浠g爜:
1 #include<iostream>
2 using namespace std;
3 class A
4 {
5 int mem1,mem2;
6 public:
7 A(const A& a);
8 A(int a, int b):mem1(a),mem2(b){}
9 int fun1(){return mem1;}
10 int fun2(){return mem2;}
11 };
12 A::A(const A& a)
13 {
14 mem1 = a.mem1;
15 mem2 = a.mem2;
16 cout << "A's copy constructor called"<<endl;
17 }
18 class B : public A
19 {
20 int mem3;
21 public:
22 B(int a, int b, int c): A(a,b),mem3(c){}
23 int fun3(){return fun1()};
24 };
25 int main()
26 {
27 B b(1,2,3);
28 A aob(b);
29 cout <<aob.fun1() << aob.fun2();//the //difference
30 }
31
榪欏氨緙栬瘧閿欒浜嗭細tess.cpp:28:36: error: request for member ‘fun2’ in ‘aob’, which is of non-class type ‘A(B)’榪欏湪涓や釜涓婅堪緙栬瘧鍣ㄩ兘鏄繖鏍風殑緇撴灉銆傞偅涔堣繖涓璞″氨鏃犳硶璋冪敤鍩虹被鐨勫嚱鏁頒簡銆?br />鎴戜釜浜鴻偆嫻呯殑鎺ㄦ柇錛欰錛圔錛夊皢琚紪璇戝櫒璁や負鏄竴縐嶆柊鐨勭被鍨嬪寰咃紝閭d箞鎬庝箞紜畾榪欑綾誨瀷鐨勬帴鍙e嚱鏁板憿錛熻繖涓嶅氨鏈夐棶棰樹簡鍚楋紵
鎴戝啀澶氭涓涓劇殑瀹為獙涓嬪涓嬩唬鐮侊細
1 #include<iostream>
2 using namespace std;
3 class A
4 {
5 int mem1,mem2;
6 public:
7 A(const A& a);
8 A(int a, int b):mem1(a),mem2(b){}
9 int fun1(){return mem1;}
10 int fun2(){return mem2;}
11 };
12 A::A(const A& a)
13 {
14 mem1 = a.mem1;
15 mem2 = a.mem2;
16 cout << "A's copy constructor called"<<endl;
17 }
18 class B : public A
19 {
20 int mem3;
21 public:
22 B(int a, int b, int c): A(a,b),mem3(c){}
23 int fun3(){return fun1();}
24 };
25 int main()
26 {
27 B b(1,2,3);
28 A aob(b);
29 cout <<aob.fun3();// the difference
30 }
31
緇撴灉鏄紪璇戦敊璇細‘class A’ has no member named ‘fun3’榪欎竴鐐逛篃涓嶆剰澶栵紝閭d箞榪欐牱鐨勫璞′笉灝辯湡鐨勬病鏈変簡鎺ュ彛浜嗭紵灝忓紵鎴戣櫄蹇冪瓑寰呭ぇ鐗涗滑鐨勮В絳旓紝甯屾湜鑳藉湪鍘熺悊涓婄粰淇轟釜瑙i噴錛屼笉鑳滄劅嬋錛?img src ="http://www.shnenglu.com/nxm1990/aggbug/168120.html" width = "1" height = "1" />
]]>