Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class A {
- public:
- A();
- A& operator=(const A& rhs)
- {
- b = rhs.b;
- return *this;
- }
- private:
- B b;
- };
- class A {
- public:
- A() : b(0)
- {
- b = new B;
- }
- A& operator=(const A& rhs)
- {
- if (this != &rhs) {
- B* b1 = 0;
- try {
- b1 = new B(*rhs.b);
- }
- catch {
- delete b1;
- throw;
- }
- delete b;
- b = b1;
- }
- return *this;
- }
- private:
- B* b;
- };
- class A {
- public:
- A() : b(0)
- {
- b = new B;
- }
- A& operator=(const A& rhs)
- {
- *b = *rhs.b;
- return *this;
- }
- private:
- B* b;
- };
- const B* A::GetB() const
- {
- return b;
- }
- A a1, a2;
- const B* my_b = a1.GetB();
- a1 = a2; // this leaves my_b dangling!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement