Visitor Pattern, Deep Copy & Double Dispatch

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
 
V

Victor Bazarov

Thomas Matthews said:
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.

Too bad you didn't show how the Copy_Visitor is going to be used.

The usual way to _replicate_ (not assign) is to have a virtual
"clone" function in the 'Field':

class Field {
public:
Field* clone() const = 0;
};

class Integer_Field {
Field* clone() const { return new Integer_Field(*this); }
};

and so on.

Of course, with your requirement to *assign*, I don't think you
will be able to avoid double dispatch. What if there *should be*
a way to assing an integer to a string (et cetera) or vice versa?

Recently somebody asked about their implementation of the virtual
assignment operator and solicited comments on it. I didn't comment,
but as I recall somebody did, there may still be some interesting
information in that thread for you.

I know this all is not of big help, sorry. When you figure it out,
could you please post again? Thanks. I will be useful for others.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top