Hi,
I'm quite confused about how to solve the following problem (search both on SO and google didn't help much).
Say we have a class defining basic operators and a simple vector as only data and add only additional methods in inherited classes:
class Foo
{
public:
// this only copies the data
Foo& operator=(const Foo& foo);
// do something that computes a new Foo from *this
Foo modifiedFoo();
//..
private:
std::vector<int> data;
}
class Bar: public Foo
{
public:
void someNewMethod();
//..
// no new data
}
Inheritance now ensures that the operator= in the case bar1 = bar2
does the right thing.
But from the data point of view, both Bar
and Foo
are basically the same, so I'd like to be able to write both
Foo foo;
Bar bar;
foo = bar; // this ...
bar = foo; // .. and this;
and more specifically
bar = foo.modifiedFoo();
[ Edit: And btw, this doesn't work either obviously...
bar1 = bar2.modifiedFoo();
]
I thought it would be as easy as adding another Bar& operator=(const Foo & foo)
in Bar
, but somehow this is ignored (and I don't like this anyways, what if I derive more and more classes?)
So what is the right way to go about this??
Thanks!! And sorry if this has been asked before.