473,378 Members | 1,544 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,378 software developers and data experts.

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

Jul 23 '05 #1
1 2293
"Thomas Matthews" <Th*************************@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
Jul 23 '05 #2

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

Similar topics

2
by: Imre Palik | last post by:
Hi, I am trying to create a framework, that automatically generates a base class for the visitor pattern: template <typename param, typename ret = void> struct visitor { typedef ret...
12
by: FluffyCat | last post by:
New on November 28, 2005 for www.FluffyCat.com PHP 5 Design Pattern Examples - the Visitor Pattern. In the Visitor pattern, one class calls a function in another class and passes an instance of...
26
by: saxenavaibhav17 | last post by:
what is Deep Copy, Shallow copy and Bitwise copy, Memberwise copy? and what is the difference between them? pls help vaibhav
13
by: blangela | last post by:
I have decided (see earlier post) to paste my Word doc here so that it will be simpler for people to provide feedback (by directly inserting their comments in the post). I will post it in 3 parts...
1
by: RedLars | last post by:
Hi, Given this class definition, public class Node { Node parent; object current; ArrayList children;
1
by: JosAH | last post by:
Greetings, this week we let go of all that algebraic stuff and concentrate a bit more on what object oriented programming is all about. Java claims to support OO, so why not use it? In this...
0
weaknessforcats
by: weaknessforcats | last post by:
Design Patterns: Visitor Introduction Polymorphism requires a class hierarchy where the interface to the hierarchy is in the base class. Virtual functions allow derived classes to override base...
3
by: paulus4605 | last post by:
dears since I'm new to ruby I try to create a form within ruby on rails. this is my controller require "player" class SpelersController < ApplicationController def index @speler =...
3
by: aaragon | last post by:
Hello everyone, I've been trying to work with the visitor design pattern, and it works fine except for the following. Let's suppose that we have a fixed hierarchy of classes (many of them)...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.