Skip to content

Instantly share code, notes, and snippets.

@slwu89
Created July 9, 2018 22:19
Show Gist options
  • Save slwu89/f69cd71bfbc49a5b9251df5619642345 to your computer and use it in GitHub Desktop.
Save slwu89/f69cd71bfbc49a5b9251df5619642345 to your computer and use it in GitHub Desktop.
constructor overloading for template classes
// Example program
#include <iostream>
#include <string>
/* Release event */
template <typename T>
class release {
public:
release(T x); /* constructor */
~release(){std::cout << "calling destructor on " << this << std::endl;}; /* destructor */
T data;
};
/* constructor */
template <>
release<double>::release(double x):data(x) {std::cout << "making a release with doubles at " << this << " with data member " << data << std::endl;};
/* destructor */
template <>
release<int>::release(int x):data(x) {std::cout << "making a release with ints at " << this<< " with data member " << data <<std::endl;};
int main()
{
release<double>* p1 = new release<double>(1.0001);
release<int>* p2 = new release<int>(1);
std::cout << "p1 has: " << p1->data << std::endl;
std::cout << "p2 has: " << p2->data << std::endl;
delete p1;
delete p2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment