473,809 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_Vis itor& cv);
/* common interface stuff */
};

typedef std::vector<Fie ld *> 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_fi eld(Integer_Fie ld& i_f);
void copy_string_fie ld(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.l earn.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 2325
"Thomas Matthews" <Th************ *************@s bcglobal.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_Vis itor& cv);
/* common interface stuff */
};

typedef std::vector<Fie ld *> 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_fi eld(Integer_Fie ld& i_f);
void copy_string_fie ld(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
1490
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 return_type; virtual return_type visit(param *) = 0;
12
3044
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 itself. The called class has special functions for each class that can call it. With the visitor pattern, the calling class can have new operations added without being changed itself.
26
15818
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
5028
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 to make it more manageable. Below is a draft of a document that I plan to give to my introductory C++ class. Please note that I have purposely left out any mention of safety issues in the ctors which could be resolved thru some combination...
1
2373
by: RedLars | last post by:
Hi, Given this class definition, public class Node { Node parent; object current; ArrayList children;
1
6533
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 week's article we're going to talk a bit about when and why to apply certain patterns. We'll start with the Visitor pattern. The pattern is also named 'double dispatch' which will become clear near the end of this little article. Here's the...
0
11634
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 class functions. Applications using polymorphism typically have functions with base class pointers or references as arguments. Then derived objects are created and used as arguments to these functions. Inside the function, only the base class methods...
3
2465
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 = Speler.find(:all, :order => 'RAND()') end
3
2315
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) which I cannot modify. I decided to use the visitor design pattern depending on the actual type of the classes because those classes already support the loki visitor. #include <Loki/Visitor.h>
0
9721
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10640
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10120
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7662
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.