chester89
chester89

Reputation: 8607

Can initialization list in constructors be used in template classes?

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])

Upvotes: 2

Views: 1911

Answers (2)

chester89
chester89

Reputation: 8607

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++").

Upvotes: 0

Jim Buck
Jim Buck

Reputation: 20724

Yes. Did the compiler tell you otherwise?

Upvotes: 4

Related Questions