T
Thomas Matthews
Hi,
I'm looking for an efficient method to deep copy
containers of fields. A Field is a parent class
with children such as Integer_Field, String_Field,
and Date_Field, etc. The algorithm / method should
ensure that an Integer_Field is not copied (assigned)
to a String_Field or other descendant of Field.
I have looked at the Visitor design pattern with
double dispatch. With this pattern, I need a separate
"visit" function for each descendant of Field. This
seems to get overly complicated.
{Yes, I've used Google to search the newsgroups.}
An example is greatly appreciated.
Here are the fundamentals:
class Copy_Visitor; /* forward declaration */
class Field
{
public:
void accept(Copy_Visitor& cv);
/* common interface stuff */
};
typedef std::vector<Field *> Record;
class Integer_Field : public Field
{
int value;
};
class String_Field : public Field
{
std::string value;
};
class Date_Field : public Field
{
unsigned int month, day, year;
};
class Copy_Visitor
{
public:
void copy_integer_field(Integer_Field& i_f);
void copy_string_field(String_Field& s_f);
void copy_date_field(Date_Field& d_f);
};
If you have another method for deep copying
descendents with safety, please post an
example.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
I'm looking for an efficient method to deep copy
containers of fields. A Field is a parent class
with children such as Integer_Field, String_Field,
and Date_Field, etc. The algorithm / method should
ensure that an Integer_Field is not copied (assigned)
to a String_Field or other descendant of Field.
I have looked at the Visitor design pattern with
double dispatch. With this pattern, I need a separate
"visit" function for each descendant of Field. This
seems to get overly complicated.
{Yes, I've used Google to search the newsgroups.}
An example is greatly appreciated.
Here are the fundamentals:
class Copy_Visitor; /* forward declaration */
class Field
{
public:
void accept(Copy_Visitor& cv);
/* common interface stuff */
};
typedef std::vector<Field *> Record;
class Integer_Field : public Field
{
int value;
};
class String_Field : public Field
{
std::string value;
};
class Date_Field : public Field
{
unsigned int month, day, year;
};
class Copy_Visitor
{
public:
void copy_integer_field(Integer_Field& i_f);
void copy_string_field(String_Field& s_f);
void copy_date_field(Date_Field& d_f);
};
If you have another method for deep copying
descendents with safety, please post an
example.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library