Corey said:
I have a class for which I needed to create an operator= function. I then
use that as a base class for another class, which also needs an operator=.
What is the syntax to use in the derived class operator= function to copy
the base classes variables in the derived class (without just copying the
code?)
A much simplified example:
class Base
{
public:
Base();
~Base();
Base &operator=(const Base &B);
protected:
int i;
}
;
Base &Base:

perator=(const Base &B)
{
if (this == &B)
return *this;
i = B.i;
return *this;
};
class Derived : public Base
{
Derived();
~Derived();
Derived &operator=(const Derived &D);
protected:
int n;
};
Derived &Derived:

perator=(const Derived &B)
{
if (this == &B)
return *this;
// None of these work!
//(Base)(*this) = :

perator=B;
//(*this) =(const Base &)B;
n = B.n;
return *this;
};
And yet, in code not directly in these classes, this works fine:
Base b;
Derived d;
d = b;
I don't see how. Derived's default ctor is private above. I suspect
that you didn't cut and paste when you posted. It adds a little bit of
confusion.
I also think that assigning a base instance to a derived instance is
rather ugly. Can be done, but I'm curious as to why you want to.
Perhaps better IMHO to create a ctor for the derived class like
Derived(const Base &b, const int v) : Base(b), n(v) {}
Please consider the following code, and also, please see
http://www.gotw.ca/gotw/059.htm
NB, I've assumed that you did in fact want Derived(), ~Derived() and
Derived &operator=(const Derived &d) to be public.
class Base
{
static int testValue() { return -99; }
public:
void swap(Base &b) {
std::swap(b.i,i);
}
Base() : i(testValue()) {}
Base(const int v) : i(v) {} // just for testing
Base(const Base &b) : i(b.i) {}
virtual ~Base() {} // virtual
Base &operator=(const Base &B);
protected:
int i;
};
Base &Base:

perator=(const Base &b)
{
Base temp(b);
swap(temp);
return *this;
}
class Derived : public Base
{
static int testValueA() { return -55; }
static int testValueB() { return -52; }
public: // So the code you provided to exercise these can compile
void swap(Derived &d) {
Base::swap(d);
std::swap(d.n,n);
}
Derived() : Base(), n(testValueA()) {}
Derived(const Derived &d) : Base(d), n(d.n) {}
Derived(const Base &b, const int v) : Base(b), n(v) {}
// a ctor like this might not be the best thing to do.
Derived(const Base &b) : Base(b), n(testValueB()) {}
~Derived() {}
Derived &operator=(const Derived &d);
Derived &operator=(const Base &b);
protected:
int n;
};
Derived &Derived:

perator=(const Derived &d)
{
Derived temp(d);
swap(temp);
return *this;
}
Derived &Derived:

perator=(const Base &b) {
// This seems rather ugly to me.
// NB that we're not preserving
// what's on the left hand side of the =
// we use to call this.
// We could play games to do that
// if that's what you want, but I tend to think
// that you'd be better off using some other syntax
// for that.
Derived temp(b);
swap(temp);
return *this;
}
int main() {
Base b(44); // changed for testing
Derived d;
d = b; // Why? What?
d = Derived(b,67); // might be better IMHO
}
I'm not clear on what you want to do. I think that someone reading your
code, maybe even you, might have to pause over d=b; What does that do?
If you want d=b to result in a change to d.i but not d.n then perhaps
you ought to consider a ctor like,
Derived(const Base &b, const Derived &d) : Base(b), n(d.n) {}
and then write
d = Derived(b,d);
which I think will communicate your intent a little better than an
unusual Derived &operator=(const Base &) ever could. IMO.
But this sort of stuff doesn't seem pretty to me, so I'd like to know,
why do you want to do this?
LR