Topic: How to invoke base class assignment operator


Author: pete@genghis.interbase.borland.com (Pete Becker)
Date: Sat, 6 Aug 1994 17:50:37 GMT
Raw View
In article <9421702.4572@mulga.cs.mu.oz.au>,
Fergus Henderson <fjh@munta.cs.mu.OZ.AU> wrote:
>
>Of course not, it's private!
>The only solution would be to contact your vendor and ask them
>to make it protected or public instead.
>

 Or ask them why it's private. There may be a good reason.
 -- Pete




Author: allen@ariel.com (Marc L. Allen)
Date: 6 Aug 1994 18:45:18 GMT
Raw View
Fergus Henderson (fjh@munta.cs.mu.OZ.AU) wrote:
: caseyw@kmsdev01.boulder.ibm.com (Casey Webster) writes:

: >My problem is that I am using a third-party base class which defines
: >several private variables and a private assignment operator.

: >My derived class defines its own assignment operator

: That is a mistake.  If the base class makes the assignment operator
: private, you should not define an assignment operator in the derived
: class.

: >I have tried everything I could think of but I am unable to invoke the
: >base class method

: Of course not, it's private!
: The only solution would be to contact your vendor and ask them
: to make it protected or public instead.

Uhhh... maybe I'm missing something, like the overloaded assignment operator
has been made virtual, but, if not, can't you simply cast both sides to the
base class and then make the assignment?

If not, then I assume that you can't assign even base classes (outside of
a base class, natch), and maybe your vendor doesn't want the base class
assigned.

Marc




Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Thu, 4 Aug 1994 16:32:49 GMT
Raw View
caseyw@kmsdev01.boulder.ibm.com (Casey Webster) writes:

>My problem is that I am using a third-party base class which defines
>several private variables and a private assignment operator.

>My derived class defines its own assignment operator

That is a mistake.  If the base class makes the assignment operator
private, you should not define an assignment operator in the derived
class.

>I have tried everything I could think of but I am unable to invoke the
>base class method

Of course not, it's private!
The only solution would be to contact your vendor and ask them
to make it protected or public instead.

--
Fergus Henderson - fjh@munta.cs.mu.oz.au




Author: caseyw@kmsdev01.boulder.ibm.com (Casey Webster)
Date: 27 Jul 1994 18:53:45 GMT
Raw View
My problem is that I am using a third-party base class which defines
several private variables and a private assignment operator.  My
derived class defines its own assignment operator but somewhere
within this assignment I need to invoke the base class operator to
insure that the private variables (including a dynamically allocated
character array) are properly handled.  I have tried everything I
could think of but I am unable to invoke the base class method, but
even when I declare it a friend I still get an error that I cannot
access the private method.  Any help would be greatly appreciated.

Simplified Problem:

class B
   {
public:
   ...
private:
   B& operator= (const B&);
   char *sz;
   ...
   };

class D : public B
   {
public:
   ...
   D& operator= (const D&);            <== how does this call B::opearator=()?
   friend B& B::operator= (const B&);  <== compiles but doesn't help!
private:
   ...
   };

                                \\/
 _I_                           (o o)                            _I_
(_@_)-----------------------ooO-(_)-Ooo------------------------(_@_)
|   |                                                          |   |
|   |  Dave (Casey) Webster            IBM Software Solutions  |   |
|   |  caseyw@ibm.vnet.com             Boulder, Colorado       |   |
|___|                            _                             |___|
(_@_)-----------------------ooO-( )-Ooo------------------------(_@_)
  I                            (o o)                             I
                                //\





Author: kocher@lts.sel.alcatel.de (Hartmut Kocher US/ESA 60/1L/2? #5629)
Date: Fri, 29 Jul 94 12:23:51 GMT
Raw View
In article <316afpINNmqn@afshub.boulder.ibm.com>, caseyw@kmsdev01.boulder.ibm.com (Casey Webster) writes:
> My problem is that I am using a third-party base class which defines
> several private variables and a private assignment operator.  My
> derived class defines its own assignment operator but somewhere
> within this assignment I need to invoke the base class operator to
> insure that the private variables (including a dynamically allocated
> character array) are properly handled.  I have tried everything I
> could think of but I am unable to invoke the base class method, but
> even when I declare it a friend I still get an error that I cannot
> access the private method.  Any help would be greatly appreciated.
>
> Simplified Problem:
>
> class B
>    {
> public:
>    ...
> private:
>    B& operator= (const B&);
>    char *sz;
>    ...
>    };
>
> class D : public B
>    {
> public:
>    ...
>    D& operator= (const D&);            <== how does this call B::opearator=()?
>    friend B& B::operator= (const B&);  <== compiles but doesn't help!
> private:
>    ...
>    };
>

This question belongs to comp.lang.C++ (see folllowup) but here it goes anyway:

1) Friendship in C++ cannot be taken, it must be granted! (Just as in
   real live :-)). Therefore, your friend declaration allows B's assignment
   operator to access _your_ class D. This is just the opposite of what
   you really wanted. To grant friendship to class D, the friend
   declaration must be in class B.

2) It is a common idiom to declare a private assignment operator to notify the
   user that this object shouldn't be copied. Most of the time, the assignment
   operator isn't even implemented. Therefore, I doubt that the B's assignment
   operator does what you expect.

Looks like you have to find a completely different solution...

--
+==============================|==============================+
| Hartmut Kocher               |                              |
| Technical Consultant         | All opinions expressed here  |
| Rational GmbH                | are my own.                  |
| Rosenstrasse 7               |                              |
| 82049 Pullach im Isartal     | I know you guessed it,       |
| Germany                      | but it keeps my lawyer happy.|
| Email: hwk@rational.com      |                              |
+==============================|==============================+