|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- # include <iostream>
- # include <string>
- using namespace std;
- struct Keyboard
- {
- string m_clavier;
- string m_mouse;
- };
- class Comstru
- {
- public:
- Comstru();
- Comstru(const Comstru& com);
- Comstru& operator =(const Comstru& com);
- Keyboard* getKeyboard();
- protected:
- Keyboard* m_pKeyboard;
- string m_strmodel;
- };
- Comstru::Comstru()
- {
- m_pKeyboard = new Keyboard;
- }
- Comstru::Comstru(const Comstru& com)
- {
- *this = com;
- }
- Keyboard* Comstru::getKeyboard()
- {
- return m_pKeyboard;
- }
- Comstru& Comstru::operator =(const Comstru& com)
- {
- if (this != &com)
- {
- m_strmodel = com.m_strmodel;
- m_pKeyboard = new Keyboard( *(com.getKeyboard()) );
- }
-
- return *this;
- }
- int main()
- {
- Comstru p1;
- (*(p1.getKeyboard())).m_clavier = "键盘";
- (*(p1.getKeyboard())).m_mouse = "鼠标";
-
- Comstru p2(p1);
- (*(p2.getKeyboard())).m_mouse = "鼠标222";
- cout << (*(p1.getKeyboard())).m_clavier << endl;
- cout << (*(p1.getKeyboard())).m_mouse << endl;
- return 0;
- }
复制代码 去掉const 关键词就正常了,为啥? |
|