0

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"
xcorat
  • 1,434
  • 2
  • 17
  • 34

3 Answers3

3

I am not sure what you are asking, but maybe the template below is what you want. Notice that template parameters go in angled brackets <>, not parentheses.

#include <iostream>
#include <vector>

template<int A, int B>
class Cls{
public:
    Cls(): a(A), b(B) { }
    void Print(){ std::cout << a << "," << b << std::endl; }
private:
    int a;
    int b;
};

int main()
{
  std::vector<Cls<3,2>> vec(5); //Something like this?
  vec[0].Print(); // Should print "3,2"
}
Jive Dadson
  • 16,680
  • 9
  • 52
  • 65
2

std::vector has a special constructor for repeating an element N times:

std::vector<Cls> vec(5, Cls(3,2));
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • That's good to know :) though I am actually looking for a general solution since I need it for a custom class that uses templates :/ – xcorat May 02 '18 at 00:03
  • 1
    @xcorat Then I think you need to restate your question. General how? – zdan May 02 '18 at 00:05
  • @xcorat It's not, at least to me. How using a template stops you from using this techinque? Something like `std::vector> vec(5, my_class(1,2));` works fine. – HolyBlackCat May 02 '18 at 00:15
0

This line makes no sense, as HoHoHo requires a type template parameter:

  HoHoHo<Cls(3,4)> hohoho_test();

I think you meant:

  HoHoHo<Cls> hohoho_test(Cls(3,4));
Toby Speight
  • 27,591
  • 48
  • 66
  • 103