I've encounter trouble attempting to implement functionality for serializing some classes during my game. I store some data inside a raw text file and I wish to have the ability to save and load to/from this. The particulars of the, however, are irrelevant. However , I'm attempting to make each object that's interesting for that save file to have the ability to serialize itself. With this I've defined an interface ISerializable, with purely virtual terms of operator<< and operator>>.

The course Hierarchy looks something similar to this

              -> GameObject -> Character -> Player ...
ISerializable               -> Item -> Container ...
              -> Room ...

What this means is you will find many possible situations for serializing the objects from the different classes. Containers, for example, should call operator<< on all contained products.

Now, since operator>> is virtual, i believed basically desired to serialize something which implements the functionality defined in ISerializable i possibly could simply do something similar to

ostream & Player::operator<<(ostream & os){
    Character::operator<<(os);
    os << player_specific_property 1 << " " 
       << player_specific_property 2 << "...";
    return os;
}

after which

ostream & Character::operator<<(ostream & os){
    GameObject::operator<<(os);
    os << character_specific_property 1 << " " 
       << character_specific_property 2 << "...";
    return os;
}

however i rapidly learnt this first attempt was illegal. What I am asking here's how do you deal with this?

I do not seem like applying a function by hand for every class. I suppose I am searching for something similar to the super functionality from Java.

Any assistance is appreciated.

-- COMMENTS ON EDIT ------------

Okay, before I had been in a rush after i was writing the question. The code has become a lot more like it had been after i attempted to compile it. I fixed the question and also the problem I'd was unrelated towards the question requested. I am embarrassed to say it had been triggered by a mistake within the wake of a big refactoring from the code, cheap the operator wasn't implemented in each and every base class.

Thank you for that replies however!

The issue is not inside your make an effort to call an online function non-virtually. The issue is this line: os = Character::operator<<(os);. That's a project, but std::ostream does not come with an operator=.

You do not need a job anyway. The stream came back is identical stream because the stream you pass in. The only real reason it's came back is so that you can chain them.

Hence the fix would be to just alter the code to

ostream & Player::operator<<(ostream & os){
    Character::operator<<(os);
    os << player_specific_property 1 << " " 
       << player_specific_property 2 << "...";
    return os;
}

This isn't how over-loading operator<< for ostream works. The left-hands operator is definitely an ostream (hence you gotta overload it as being a totally free function) and also the right-hands operator is the object (and that's why the virtual mechanism wouldn't easily work.

I guess you could attempt:

class Base
{
    //...
    virtual std::ostream& output(std::ostream&) const;
};

std::ostream& operator<< (std::ostream& os, const Base& obj)
{
    return obj.output(os);
}

Now a derived class naturally might call the output approach to its parent(s):

class Derived: public Base
    //...
    virtual std::ostream& output(std::ostream& os) const
    {
        Base::output(os);
        return os << my_specific_data;
    }
};