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.
Martin told you what went wrong, here’s what you should do instead:
Now
foois a reference to a an object, and you can have base class references (and pointers, BTW) refer to objects of derived classes.However, this
will never work (at least not implicitly) and it shouldn’t. What you’re trying to do here is to assign a base class object (or reference) to a derived class.
However, while a derived class can always stand in for a base class object, the opposite is not true. (You can’t just mindlessly use any vehicle when what you need is a boat, because not all vehicles are boats, the vehicle you’re using might be a car and you’d drown.)
This is not to say that you cannot make it work. if you know how to create a derived class object from a base class object, you can write a function to do so. It’s just that the compiler doesn’t know how to make a car from a vehicle.
As solipist has shown, such a conversion function can be a constructor. However, I would make conversion constructors explicit:
The
explicitkeyword makes sure that the compiler will never attempt to call this constructor unless you say so. If you remove it, then this code would compile:and the compiler would silently generate this code:
f(Bar(foo));While this seems handy at first, I think I have sooner or later been bitten by every single implicit conversions I allowed to creep into my code and had to remove them later. So many years ago I swore to never allow them anymore.
Note that even with the
explicitconversion constructor you can still callf(Bar)with aFooobject – you just have to say so explicitly: