Как я могу использовать шаблонный класс, где я могу вызвать конструктор с некоторыми аргументами по умолчанию? Возможно ли это?
Пример:
#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;
}
Здесь я хотел бы иметь возможность вызвать конструктор класса шаблона с некоторыми значениями по умолчанию. Как мне добиться чего-то подобного?
Я могу написать другой класс для инкапсуляции, но надеюсь, что есть более умное решение.
Я думаю, что способ сделать это инкапсуляции. thx @ jive-dadson
template<int A, int B> class Cls_ext: public Cls {
public:
Cls_ext<A,B>(){ this->a = A; this->b = B;}
};
и
HoHoHo<Cls_ext<3,4> > hohoho_test;
cout << hohoho_test << endl; // prints "HoHoHo 3,4"