Visitor Pattern, Deep Copy & Double Dispatch



 DEVELOP > c-Plus-Plus > Visitor Pattern, Deep Copy & Double Dispatch

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Thomas Matthews"
Date: 21 Feb 2005 01:31:26 PM
Object: Visitor Pattern, Deep Copy & Double Dispatch
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
.

User: "Victor Bazarov"

Title: Re: Visitor Pattern, Deep Copy & Double Dispatch 21 Feb 2005 02:50:57 PM
"Thomas Matthews" <Thomas_MatthewsSpamBotsSuck@sbcglobal.net> wrote...

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
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER