AceInfinity
Emeritus, Contributor
Fancy title :lolg:
The task is to create an implementation of the class I gave an outline of in the following code:
Such that you get the expected output of: 10 11 12 13 14 15 16 17 18 19 20
Rules:
- You can't modify anything inside the main function.
- All you can do is work within the boundaries of the _BaseInt class to make sure that it provides the functionality needed to get the expected output.
For those of you who want to see the solution, see the following:
*SPOILER* - If you are trying this challenge, do not peek until you've given up or came up with a solution of your own.
Good luck :thumbsup2:
The task is to create an implementation of the class I gave an outline of in the following code:
Code:
[NO-PARSE]#include <iostream>
#include <vector>
#include <algorithm>
template <typename T>
class _BaseInt
{
// Your implementation here
};
int main(void)
{
const unsigned max = 5;
unsigned n = 10;
_BaseInt<unsigned> m(&n);
std::vector<_BaseInt<unsigned>> v;
std::fill_n(std::back_inserter(v), max + 1, m);
std::fill_n(std::back_inserter(v), max, m);
std::copy(v.begin(), v.end(), std::ostream_iterator<unsigned>(std::cout, " "));
std::endl(std::cout);
return 0;
}[/NO-PARSE]
Such that you get the expected output of: 10 11 12 13 14 15 16 17 18 19 20
Rules:
- You can't modify anything inside the main function.
- All you can do is work within the boundaries of the _BaseInt class to make sure that it provides the functionality needed to get the expected output.
For those of you who want to see the solution, see the following:
*SPOILER* - If you are trying this challenge, do not peek until you've given up or came up with a solution of your own.
Read More:
Code:
[NO-PARSE]#include <iostream>
#include <vector>
#include <algorithm>
template <typename T>
class _BaseInt
{
public:
_BaseInt<T>(T *val) { value = &(--(*val)); }
operator T () { return ++(*value); }
private:
T *value;
};
int main(void)
{
const unsigned max = 5;
unsigned n = 10;
_BaseInt<unsigned> m(&n);
std::vector<_BaseInt<unsigned>> v;
std::fill_n(std::back_inserter(v), max + 1, m);
std::fill_n(std::back_inserter(v), max, m);
// Expected Output: 10 11 12 13 14 15 16 17 18 19 20
std::copy(v.begin(), v.end(), std::ostream_iterator<unsigned>(std::cout, " "));
std::endl(std::cout);
return 0;
}[/NO-PARSE]
Good luck :thumbsup2: