- Joined
- 11/5/14
- Messages
- 295
- Points
- 53
Hi all,
I have written a C++ program that passes an array of 10 EuropeanOptions, by reference to a function intrinsicValues(). The call and the function definition is as below.
The first argument to instrinsicValues(), EuropeanOption (&opt) [10] can be understood as, reference to EuropeanOption[10].
Now, references in C++ are like constant pointers that are automatically dereferenced. So, why can't the compiler be happy with EuropeanOption& myOpt? Ultimately, all I need is a handle to myOpt in memory to access the array. Or that's just standard C++ syntax?
I have written a C++ program that passes an array of 10 EuropeanOptions, by reference to a function intrinsicValues(). The call and the function definition is as below.
The first argument to instrinsicValues(), EuropeanOption (&opt) [10] can be understood as, reference to EuropeanOption[10].
Now, references in C++ are like constant pointers that are automatically dereferenced. So, why can't the compiler be happy with EuropeanOption& myOpt? Ultimately, all I need is a handle to myOpt in memory to access the array. Or that's just standard C++ syntax?
Code:
class EuropeanOption
{
public:
double sigma, r, S, T, K;
char type;
EuropeanOption()
{
S = 100; T = 1; r = 0.10; sigma = 0.05; K = 100; type = 'C';
}
};
Code:
//We can create an array of instances
EuropeanOption myPortfolio[10];
for (int j = 0; j < 10; j++)
{
myPortfolio[j].K = 105 - j;
myPortfolio[j].print();
}
intrinsicValues(myPortfolio,10);
Code:
void intrinsicValues(EuropeanOption (&opts) [10], int size)
{
double value = 0;
for (int i = 0; i < size; i++)
{
if (opts[i].type == 'C')
{
value = (opts[i].S >= opts[i].K ? opts[i].S - opts[i].K : 0);
std::cout << "Intrinsic value = " << value << std::endl;
}
if (opts[i].type == 'P')
{
value = (opts[i].S <= opts[i].K ? opts[i].K - opts[i].S : 0);
std::cout << "Intrinsic value = " << value << std::endl;
}
}
}