How can I use a template class where I can call a constructor with some default arguments? Is this possible?
Ex:
#include <iostream>
#include <string>
using namespace std;
template <class T> class HoHoHo {
public:
HoHoHo<T>(T yell);
template <class TT> friend std::ostream& operator<<(std::ostream&, const HoHoHo<TT> &);
private:
T yell;
};
template <class T> HoHoHo<T>::HoHoHo(T yell) {this->yell = yell;}
template <class T> std::ostream& operator<<(std::ostream &o, const HoHoHo<T> &val){ return o << "HoHoHo " << val.yell << "."; }
class Cls{
public:
Cls(int a=0, int b=0);
friend std::ostream& operator<<(std::ostream&, const Cls &);
private:
int a;
int b;
};
Cls::Cls(int a, int b){ this->a = a; this->b = b; }
std::ostream& operator<<(std::ostream &o, const Cls &val) { return o << val.a << "," << val.b; }
int main()
{
Cls cls(2,3);
HoHoHo<Cls> hohoho(cls);
cout << hohoho << endl; //This prints "HoHoHo 2,3"
//DO NOT WORK
HoHoHo<Cls(3,4)> hohoho_test(); // Want something like this?
cout << hohoho_test << endl; // to print "HoHoHo 2,3"
return 0;
}
Here I would like to be able to call the constructor of the template class with some default values. How do I achieve something like this?
I can write another class to encapsulate, but hoping there's a more clever solution.
I guess the way to do this IS encapsulation. thx @jive-dadson
template<int A, int B> class Cls_ext: public Cls {
public:
Cls_ext<A,B>(){ this->a = A; this->b = B;}
};
and
HoHoHo<Cls_ext<3,4> > hohoho_test;
cout << hohoho_test << endl; // prints "HoHoHo 3,4"