473,387 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

[IO library] Comment on the following c++ design

Hello,

I am looking for suggestions on the following design (*). Basically
I am dealing with objects of type A, which can be serialized as 2
differents representations: B or C. I like this approach because B/C
can directly have access to the internal of A (which is required for
serialization). It also make it easy for user to add a new
representation D.

For the curious, I am writing a file format library and I am trying
to deal with third-party implementation bug. I know how to deal with
certain bugs, but they add a nonnegligable overhead if one is trying
to deal with them in the general case (B/C). Making them separate
classes make it easy for user to include/exclude them.

Thanks for comments,
-Mathieu

(*)
#include <iostream>

struct A
{
int a;
template <typename T>
void dump(std::ostream &os) {
static_cast<T&>(*this).dump(os);
}
};

struct B : public A
{
void dump(std::ostream& os) {
std::cout << "B:" << a << std::endl;
}
};

struct C : public A
{
void dump(std::ostream &os) {
std::cout << "C:" << a << std::endl;
}
};

int main()
{
A a = {2};
a.dump<B>(std::cout);
a.dump<C>(std::cout);
return 0;
}

Nov 11 '07 #1
1 1129
On 12 , 00:46, mathieu <mathieu.malate...@gmail.comwrote:
Hello,

struct B : public A
{
void dump(std::ostream& os) {
std::cout << "B:" << a << std::endl;
}

};

struct C : public A
{
void dump(std::ostream &os) {
std::cout << "C:" << a << std::endl;
}

};
1) i believe you meant os instead of std::cout here
2) as i understood you have several ways to format your class/struct A
so why not separate the formatting algorithm into the hierarchy
with
the following base class

class IFormatter
{
public:
virtual void Dump( std::ostream &os, const A& a ) = 0;
};

.....
This appears to be a simple Strategy Pattern

You should also add method to A which returns all information
which is necessary for serialization in some unified form
( perhaps an associate container "name"-"value" would fit )
this will preserve the incapsulation and decouple the formatting
algorithms from the internal representation of A. Thus you will be
able to modify formatting algorithms and A independently.
int main()
{
A a = {2};
a.dump<B>(std::cout);
a.dump<C>(std::cout);
return 0;
3) You should avoid casting A to B,C
>
}- -

- -

Nov 11 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Paul Brun | last post by:
Hi guys, I would like to find out if : 1) Is the above possible? I have tried to wrap a C library that our company produces in a .Net class library and am receiving the following error: ...
43
by: Steven T. Hatton | last post by:
Now that I have a better grasp of the scope and capabilities of the C++ Standard Library, I understand that products such as Qt actually provide much of the same functionality through their own...
0
by: jason | last post by:
i have classic ASP code that is calling a C# class library, which is wrapped for COM interop, and registered in the COM+ MMC. i have written 3 objects for the class library so far, and all three...
5
by: Paul Brun | last post by:
Hi guys, I would like to find out if : 1) Is the above possible? I have tried to wrap a C library that our company produces in a .Net class library and am receiving the following error: ...
5
by: Tony Johansson | last post by:
Hello! I have one solution file that consist of three project. One project that build the exe file called A One project that build a user control dll. Here we have a class called B One project...
0
by: tony | last post by:
Hello! This is a rather long mail but it's a very interesting one. I hope you read it. I have tried several times to get an answer to this mail but I have not get any answer saying something...
7
by: gizax | last post by:
Hi guys, I've the pleasure to have a comment from David Hansson creator of Ruby on Rail about AHAH section. > http://www.gizax.it/ahahsection/ Here follow his comment: » 19 May 2006 "My...
0
by: drawing in aspnet | last post by:
Question about putting the data layer in a separate class library. I keep reading that the data layer should be separated from the presentation layer and put in its own class library. I am...
14
by: Siv | last post by:
Hi, Just busily coding away and removed a procedure from my code and all of a sudden an error came up miles away from the location of the piece of code I removed and it relates to the XML...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.