home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!ogicse!decwrl!pacbell.com!pjcondi
- From: pjcondi@ns.pacbell.com (Paul Condie)
- Newsgroups: comp.lang.c++
- Subject: Re: operator = with inheritance
- Message-ID: <1993Jan26.175530.1382@PacBell.COM>
- Date: 26 Jan 93 17:55:30 GMT
- Article-I.D.: PacBell.1993Jan26.175530.1382
- References: <1993Jan14.185649.21213@murdoch.acc.Virginia.EDU>
- Sender: news@PacBell.COM (Pacific Bell Netnews)
- Organization: Pacific * Bell
- Lines: 61
- X-Newsreader: TIN [version 1.1 PL8]
-
-
-
- What would be wrong with this implementation:
-
-
- #include <iostream.h>
-
- class A
- {
- public:
- A (int arg = 0){
- a = arg;
- }
-
- virtual A & operator = (const A & rhs) {
- a = rhs.a;
- return *this;
- }
-
- private:
- int a;
- };
-
-
- class B : public A
- {
- public:
- B (int arg = 0) : A(arg) {
- b = arg;
- }
-
- A & operator = (const A & rhs) {
- A::operator = (rhs);
-
- // The following cast should be ok because we know
- // we have a B object because the virtual mechanism
- // has told us. right?
-
- b = ((B &)rhs).b;
-
- return *this;
- }
-
- private:
- int b;
- };
-
-
-
- main ()
- {
- A * obj3 = new B (1);
- A * obj4 = new B;
-
- *obj4 = *obj3; // works for pointer types
-
- B obj1 (2);
- B obj2;
-
- obj2 = obj1; // works for object types
- }
-