Re: Question on private virtual methods
On 7/17/2012 8:02 PM, plegdfmds@gmail.com wrote:
I have a question on how to structure some private virtual methods.
MY problem is: I have some classes I need to serialize on a vector
of
bytes to be transmitted (and then reconstructed on the other end). What
I want to do is having a class "Serializable" (inherited by all
serializable classes) that specifies the interface of the
(de)serialization mechanism, while the implementation is left to some
private virtual methods that the derived classes need to implement. For
example, for a class A:
class Serializable
{
public:
Bytestream serialize();
void deserialize(Bytestream&);
private:
virtual Bytestream serializeStep1() = 0;
virtual void deserializeStep1(Bytestream&) = 0;
};
Bytestream Serializable::serialize() {
return serializeStep1();
}
void Serializable::deserialize(Bytestream&) {
deserializeStep1(Bytestream&);
}
I don't think it's warranted. Or, your class is misnamed, and should be
named 'SerializableInStep1'.
class A: public Serializable
{
private:
virtual Bytestream serializeStep1();
virtual void deserializeStep1(Bytestream&);
};
and A implements serializeStep1 e deserializeStep1 depending on its
internal members.
Now, my problem is: class A is itself inherited by classes B!, B2
etc.
I'd like to build a "cascaded serialization": in the serialization of
B1, first A is serialized and then the internals of B1 are serialized
and appended to the Bytestream.
Looks like B1's implementation should just call the base class'
*protected* member.
How to do this? The only thing I can think of is a cascade of
private
methods like this:
class A: public Serializable
{
private:
virtual Bytestream serializeStep1();
virtual Bytestream serializeStep2() = 0;
virtual void deserializeStep1(Bytestream&);
virtual void deserializeStep2(Bytestream&) = 0;
};
Bytestream A::serialize() {
return serializeStep1() + serializeStep2(); // il '+' concatena
}
void A::deserialize(Bytestream&) {
deserializeStep1(Bytestream&);
deserializeStep2(Bytestream&);
}
class B1: public A
{
private:
virtual Bytestream serializeStep2();
virtual void deserializeStep2(Bytestream&);
}
but it doesn't look good, because now I need to structure A
differently depending whether it's inherited or not.
A couple of comments. Generally, design is out of scope in a language
newsgroup, unless it's language-specific, and here it's not. Second,
your 'Serializable' class imposes an unnecessary limitation on the
derived classes, IMO, to implement the 'step1' interface. What for?
Just force the derived classes to implement 'serialize' and 'de-'
methods, and let them decide how to perform [de]serialization. Third,
if you want sequential serialization, perhaps a policy is a better way.
See 'Modern C++ Design' by Alexandrescu.
Perhaps if you elaborated a bit more on *why* step1 and step2 are the
model requirements, it would be a bit more obvious... I know, it's not
much of help, but without knowing the underlying model, it's kind of
hard to recommend any particular solution or even evaluate the presented
solution. Sorry.
V
--
I do not respond to top-posted replies, please don't ask