6.6.4 Con trỏ hằng
Con trỏ hằng – constant pointers Các con trỏ được cho phép khai báo là const. Cú pháp được sử dụng cho các khai báo này […]
|
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 |
#include <iostream> using namespace std; class A { public: A(A &src) { cout << "copying A..." << endl; } A(void) { } void Do(void) { cout << "A is doing something" << endl; } }; class B { public: B(B &src) { cout << "copying B..." << endl; } B(void){ } void Do(void) { cout << "B is doing something" << endl; } }; class Compo { public: Compo(Compo &src) { cout << "Copying Compo..." << endl; } Compo(void) { } ; A f1; B f2; }; int main(void) { Compo co1; Compo co2 = co1; co2.f1.Do(); co2.f2.Do(); return 0; } |
|
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 |
#include <iostream> using namespace std; class A { public: A(A &src) { cout << "copying A..." << endl; } A(void) { } void Do(void) { cout << "A is doing something" << endl; } }; class B { public: B(B &src) { cout << "copying B..." << endl; } B(void){ } void Do(void) { cout << "B is doing something" << endl; } }; class Compo { public: Compo(void) { } ; A f1; B f2; }; int main(void) { Compo co1; Compo co2 = co1; co2.f1.Do(); co2.f2.Do(); return 0; } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> using namespace std; class WithConstructor { public: int i; float f; WithConstructor(int a = 0, float b = 0) : i(a), f(b) { } void Display(void) { cout << "i=" << i << ",f=" << f << endl; } }; int main(void) { WithConstructor o1; WithConstructor *o2; o2 = new WithConstructor; o1.Display(); o2 -> Display(); return 0; } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> using namespace std; class WithConstructor { public: int i; float f; WithConstructor(int a, float b) : i(a), f(b) { } void Display(void) { cout << "i=" << i << ",f=" << f << endl; } }; int main(void) { WithConstructor o1; WithConstructor *o2; o2 = new WithConstructor; o1.Display(); o2 -> Display(); return 0; } |
Copyright © 2025 CppDeveloper by Phạm Minh Tuấn (SHUN)