Skip to main content
gamedev.net
🔒 Locked

Inheritance with Serializable classes

Started by russian-bear Jun 1, 2003 at 7:44 AM 0 replies 550+ views
Original Post
russian-bear
russian-bear
Hi! I have a litle problem here. I have a base class a Serializable interface and a class derived from the base class, like this:
class Serializable
{
  public:
	  virtual void Serialize( std::ostream& out) const = 0;
	  virtual void DeSerialize( std::istream& in) = 0;
};

class Base : public Serializable  
{
public:
	Base();
        ~Base();
	void Serialize (std::ostream& out) const;
	void DeSerialize (std::istream& in);
protected:
	float _member1, _member2;
};

class Derived : public Base
{
public:
	Derived();
        ~Derived();
protected:
        float _member3;
}
Ok so far so good. Here comes the problem: i want to be able to serialize _member3 in Derived class. Probably by overloading the Serialize / DeSerialize functions. But then i must also serialize _member1 and _member2 like in the base class or call the serialize functions from the base class before i store _member3. Is there a nicer way of doing this ? < There is no stupid questions, only stupid answers! >
< There are no stupid questions, only stupid answers! >
C-Junkie
C-Junkie
quote:
Original post by russian-bear
Here comes the problem: i want to be able to serialize _member3 in Derived class. Probably by overloading the Serialize / DeSerialize functions. But then i must also serialize _member1 and _member2 like in the base class or call the serialize functions from the base class before i store _member3. Is there a nicer way of doing this ?



That is the nice way. Its pretty, look:


void Derived::Serialize (std::ostream& out) const
{
Base::Serialize(out);

out << _member3 <<endl;
}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.