Constructor in Antithetic.cpp (Ch 6)

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

Constructor in Antithetic.cpp (Ch 6)

Postby fmincon » Tue Aug 14, 2012 2:35 am

Question about the initialization of the constructor in Antithetic.cpp:

Code: Select all
AntiThetic::AntiThetic(const Wrapper<RandomBase>& innerGenerator )
            : RandomBase(*innerGenerator),
              InnerGenerator(innerGenerator)


Do you mind explaining
Code: Select all
RandomBase(*innerGenerator)
?

From a most basic perspective, I'm having trouble understanding the syntax, because I thought in the initialization of a constructor, the statements that come after the single colon were always in the form of some
Code: Select all
private_member(argument for private_member)
, but RandomBase isn't explicitly a private member of Antithetic, but it's the wrapper type. After reading the top paragraph on page 97 of your book, I am still confused as how this line works. (and what happens if you leave it out?) Also, the way the parameter
Code: Select all
const Wrapper<RandomBase>& innerGenerator
is defined requires the argument innerGenerator to be of class type RandomBase, hence when
Code: Select all
RandomBase(*innerGenerator)
is called, I'm assuming this is just calling a default copy constructor that wasn't explicitly implemented in the original Random2.h... is this line of reasoning correct?

Last but not least, at the end of Chapter 6 when the driver file RandomMain3.cpp is run, an AntiThetic object called GenTwo with argument of type RandomParkMiller called generator is fed into a SimpleMonteCarlo6 object, which invokes the method
Code: Select all
GetGaussians(VariateArray)
. Since the AntiThetic class doesn't have a GetGaussians method, it just uses the one supplied by its base class RandomBase right?

Thank you!
fmincon
 
Posts: 2
Joined: Tue Aug 14, 2012 2:15 am

Re: Constructor in Antithetic.cpp (Ch 6)

Postby fmincon » Tue Aug 14, 2012 8:08 pm

Oh I think I figured it out. So I forgot that AntiThetic is an inherited class under RandomBase, hence when it is initialized, its parent class also needs to be initialized, especially in this case since RandomBase has a private data member Dimensionality which needs to be fed by the innerGenerator argument. So, when the code
Code: Select all
RandomBase(*innerGenerator)
is run, the default copy class constructor of RandomBase copies the unsigned long Dimensionality private member for the AntiThetic instance being generated. I assume that innerGenerator is a wrapped pointer in this case only because we make it so with the parameter being
Code: Select all
const Wrapper<RandomBase>& innerGenerator
because in the driver file RandomMain3.cpp, the object being fed into the parameter is just a RandomParkMiller object that isn't wrapped (more specifically, it's the pointer of a RandomParkMiller object).
fmincon
 
Posts: 2
Joined: Tue Aug 14, 2012 2:15 am

Re: Constructor in Antithetic.cpp (Ch 6)

Postby mj » Thu Aug 23, 2012 1:15 am

that's right.

More generally, any member or base can be initialized in the initializer list by using any of its constructors including default compiler
provided ones.
mj
Site Admin
 
Posts: 1341
Joined: Fri Jul 27, 2007 7:21 am


Return to C++ design patterns and derivatives pricing

Who is online

Users browsing this forum: No registered users and 1 guest

cron