0
class PayOffBridge
{
public:

    PayOffBridge();
    PayOffBridge(const PayOffBridge& original);
    PayOffBridge(const PayOff& innerPayOff);

    inline double operator()(double Spot) const;
    ~PayOffBridge();
    PayOffBridge& operator=(const PayOffBridge& original);

private:

    PayOff* ThePayOffPtr;

};

and another class with a member which is an object of class PayOffBridge:

class VanillaOption
{
public:

    VanillaOption(const PayOffBridge& ThePayOff_, double Expiry);

    double OptionPayOff(double Spot) const;
    double GetExpiry() const;

private:

    double Expiry;
    PayOffBridge ThePayOff;
};

The PayOff* ThePayOffPtr in PayOffBridge is a pointer to the following pure virtual abstract class:

class PayOff
{
public:

    PayOff(){};

    virtual double operator()(double Spot) const=0;
    virtual ~PayOff(){}
    virtual PayOff* clone() const=0;

private:

};

The concrete class PayOffCall is derived from PayOff. In main() I have

PayOffCall thePayOff(Strike);//double Strike
VanillaOption theOption(thePayOff, Expiry);//double Expiry

When I step through the code using F11 in Visual Studio, the line VanillaOption theOption(thePayOff, Expiry); ends up calling PayOffBridge(const PayOff& innerPayOff);. I cannot figure out from where this gets called. How does the constructor for VanillaOption end up calling this?

My 2nd question is the constructor for VanillaOption which gets called from main() is

VanillaOption::VanillaOption(const PayOffBridge& ThePayOff_, double Expiry_): ThePayOff(ThePayOff_), Expiry(Expiry_)
{
}

What exactly does ThePayOff(ThePayOff_) do? That is, which constructor of PayOffBridge gets called, if at all, and what exactly does this syntax accomplish?

Tryer
  • 3,580
  • 1
  • 26
  • 49

2 Answers2

2

1st answer

VanillaOption expects PayOffBridge object as a constructor parameter. But, you pass PayOffCall object instead. Compiler looks for a way to construct temporary PayOffBridge object from given PayOffCall object.

It has PayOffBridge(const PayOff& innerPayOff); constructor. So, it needs a reference to PayOff object for construction. But, PayOffCall is derived from PayOff, so const PayOffCall& is implicitly converted into const PayOff& and you get PayOffBridge constructed.

2nd answer

ThePayOff(ThePayOff_) means object construction. For a start you should recognize types of these variables.

const PayOffBridge& ThePayOff_ - input parameter

PayOffBridge ThePayOff - member of VanilaOption

So, PayOffBridge object is constructed from another PayOffBridge object. Obviously, copy constructor is called

PayOffBridge(const PayOffBridge& original);

Stas
  • 11,571
  • 9
  • 40
  • 58
  • Wow...I guess that's it then...the compiler goes down the list of possible constructors for `PayOffBridge` that could use `PayOffCall` Object...The one I encountered is the "closest" one...This opens another set of questions...what happens when there is a tie for the "closest" one? – Tryer Dec 04 '10 at 22:08
  • Thanks for the clarification...this makes my previous comment moot. – Tryer Dec 04 '10 at 22:22
  • @Tryer C++ has strict rules of which overloaded constructor or method should be called. If there are two or more constructors fit equally, you just get a compilation error. – Stas Dec 04 '10 at 22:23
1

The constructor for VanillaOption that you use has an initialization list to construct it's internal members. This is invoking the copy constructor for PayoffBridge, copying ThePayoff_ into ThePayoff

VanillaOption::VanillaOption(const PayOffBridge& ThePayOff_, double Expiry_):
    ThePayOff(ThePayOff_), 
    Expiry(Expiry_)
{
}

The 'body' of this constructor is intentionally empty because all the work is done by the initialization list.

Roddy
  • 66,617
  • 42
  • 165
  • 277
  • Hmm...but `PayOffBridge(const PayOff& innerPayOff)` is not a copy constructor, right? It applies to a `PayOffBridge` object yet its parameter is a `PayOff` object. – Tryer Dec 04 '10 at 22:03
  • Oops. Yes, I'll correct that.But it doesn't make a real difference. Rember inheritance is about "is a", so your PayoffCall 'is a' PayOff', so that constructor can be used unambiguously. – Roddy Dec 04 '10 at 22:14