Can initialization list in constructors be used in template classes?
-
04-07-2019 - |
Question
I find that most books concerning C++ templates don't tell anything about whether it's possible or not to use initialization list in constructor of a template class.
For example, I have code like this:
template <class T>
class Stack {
T* data;
std::size_t count;
std::size_t capacity;
enum {INIT = 5};
public:
Stack() {
count = 0;
capacity = INIT;
data = new T [INIT];
}
Can I replace the constructor with
Stack(): count(0), capacity(INIT), data(new T [INIT])
Solution
Yes. Did the compiler tell you otherwise?
OTHER TIPS
I've just tried and VS2008 says that it's OK, but it seems a little bit strange because some great authors don't do that (Eckel, for example, in his "Thinking in C++").
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow