-
default template?
Here is the code:
Code:
class out {
public:
int test;
};
class out2 : public out {
public:
int test2;
};
template <class a_type>
class cone {
public:
a_type cobject;
};
class ctwo : public cone<out2> {
public:
int test;
};
Can I have default template for templated class 'cone'?
So compiler would use default_class if I dont specify any..
For instance:
Code:
class default_class {
}
template <class a_type = default_class>
class cone {
public:
a_type cobject;
};
class ctwo : public cone {
public:
int test;
};
-
Did you try it? What errors did you get?
I think this is what you want:
Code:
class ctwo : public cone<> {
-
Code:
#include <iostream>
#include <string>
class foo {
public:
std::string toString() {
return "Hello World";
}
};
class bar {
public:
std::string toString() {
return "Goodbye!";
}
};
template <typename A = foo> class Printer {
public:
A myA;
void print() {
std::cout << myA.toString() << '\n';
}
};
int main (void) {
Printer<> hello; // Uses foo by default
hello.print(); // Prints "Hello World!" using foo
Printer<foo> hello2; // Explicitly uses foo
hello2.print(); // Prints "Hello World!" using foo
Printer<bar> bye; // Explicitly uses bar
bye.print(); // Prints "Goodbye!" using bar
return 0;
}
-