Question on const, Chap 4, Bridge

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

Question on const, Chap 4, Bridge

Postby erwang2007 » Sat Sep 05, 2015 2:05 am

Hi, I'm reading about the bridge pattern codes in chapter 4.5.

Here is an interesting compile error: No matching constructor for initialization of "VanillaOption".

The line with error is below:
VanillaOption theOption(thePayOff, Expiry);

The reason is because I delete the const in "VanillaOption::VanillaOption(const PayOffBridge& payoff, double expiry)".
Why the "const" keyword in "OptionVanilla::OptionVanilla(const PayoffBridge& payoff, double expiry)" is so important to make it work?

Is it because theOption(thePayOff, Expiry) called both constructor and copy constructor of PayOffBridge, and copy constructor request const input?

Thanks.
erwang2007
 
Posts: 1
Joined: Fri Sep 04, 2015 11:24 pm

Re: Question on const, Chap 4, Bridge

Postby mj » Sat Jun 04, 2016 10:15 am

there's a rule in C++ that you can't pass a temporary object to a non-const & reference but you can to a const one. I suspect that that is the issue here .
mj
Site Admin
 
Posts: 1380
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