Ch. 5.5 (p.80 in Ed. 2) Conversion from object to wrapper

This forum is to discuss the book "C++ design patterns and derivatives pricing."

Ch. 5.5 (p.80 in Ed. 2) Conversion from object to wrapper

Postby ColdKing » Thu Jun 09, 2011 12:05 am

In the 2008 Edition 2, Chapter 5.5, page 80, Listing 5.9, the code says:

StatisticsMean gatherer;
ConvergenceTable gathererTwo(gatherer);

And below that there is a comment "Note the constructor takes a Wrapper<MCStatistics> object but the compiler happily does this conversion for us." I don't know why this can happen. Basically, the constructor is: ConvergenceTable(const Wrapper<StatisticsMC>& Inner_) How could a object be converted to its wrapper? The wrapper has its only member: a pointer to the base_class (for polymorphism use). If the automatic conversion of the compiler can happen, that means the compiler could directly get the address of the object and save it to that pointer member of the wrapper? Sounds strange...

It is also possible that an input of the StatisticsMean object triggers a copy constructor in Wrapper<StatisticsMC> which takes an argument of "const StatisticsMC &" type and closes a copy inside it self. However, in this case, gathererTwo actually operates on a copied version of gatherer, rather than gatherer itself. That seems a violation of the initial intention of design.

Thanks for clarifying this conversion problem for me!
ColdKing
 
Posts: 7
Joined: Wed Jun 08, 2011 11:49 pm

Re: Ch. 5.5 (p.80 in Ed. 2) Conversion from object to wrapper

Postby mj » Thu Jun 09, 2011 3:16 am

the compiler if it sees a constructor

obj::obj(const X& input)

and a call

Y inputObject;
obj myobj(inputObject)

will first try to see if Y is X or derived from . If not it will look to see if there's a constructor for X
that takes a Y and turns it into an X. I think the maximum length of such an implicit chain is 2.
mj
Site Admin
 
Posts: 1380
Joined: Fri Jul 27, 2007 7:21 am

Re: Ch. 5.5 (p.80 in Ed. 2) Conversion from object to wrapper

Postby singlau714 » Sat Dec 01, 2012 10:34 am

I have a kinda similar problem as ColdKing, where he pointed out that "Due to the wrapper used, the gathererTwo actually operates on a copied version of gatherer, rather than gatherer itself". Certainly it will work fine with the decorated class of CovergenceTable.

However, when I am trying to do the following two tasks some concerns come up my mind:

1) I have built an inherited class to gather the first four moments already (named as "StatisticsMoments"), then I wanted to build another inherited class to gather the standard errors using the first two moments from the "StatisticsMoments object". If I wrote the constructor like "StatisticsStdE(const Wrapper<StatisticsMC>& Inner_)" then it will work on a copied version of the original object. That means the update (namly the "DumpOneResult" method) within each loop needs to be done on both the "StatisticsMoments" and "StatisticsStdE" objects, which will apparently double the computing cost and not desirable. Is there a better way to do the task with the wrapper, such that the two objects are not isolated to each other?

2) I am trying to write a terminator (in addition to the two terminators in Ex 5.4), which is conditional on the standard errors. A wrapper of the error gatherer will certainly do the job, but again its wrapped object would be a copied version of the error gatherer. And that means it has to be updated in each loop in addition to the updates being done on the original gather wrapped by the "ConvergenceTable". I want both the error terminator and the convergence table to work simultaneously within the Monte Carlo loop, but surely not working as separated objects. Will there be an alternative solution using wrapper class?

Many thanks !! The C++ book of Mark is just great ! especially for someone with a relatively weak background in programming like me.
singlau714
 
Posts: 2
Joined: Sat Dec 01, 2012 9:48 am

Re: Ch. 5.5 (p.80 in Ed. 2) Conversion from object to wrapper

Postby mj » Sun Dec 09, 2012 8:41 am

I think you really want straight forward object composition here rather than pointers to base classes.

If you know you want a data member of type StatisticsMoment then you just put in a datamember of that type.

you could add a getPathsDone() method to statisticsMoment so that you don;t need to store any extra info,

re 2) well you could redesign the code to have one class of objects for both gathering and termination
mj
Site Admin
 
Posts: 1380
Joined: Fri Jul 27, 2007 7:21 am

Re: Ch. 5.5 (p.80 in Ed. 2) Conversion from object to wrapper

Postby dustinmoskovitz » Thu Oct 24, 2013 6:31 am

Good job,I appreciate your work because i have the same problem,but your post solve my problem.Thanks for sharing information,your information increase my knowledge.
DASGHFDAS
dustinmoskovitz
 
Posts: 1
Joined: Thu Oct 24, 2013 6:27 am


Return to C++ design patterns and derivatives pricing

Who is online

Users browsing this forum: No registered users and 1 guest

cron