0

My question is simple: What's the point in defining elements in the function template declaration versus doing it in the body?

As an example, a snippet from my textbook.

template<class ItemType>
class ArrayBag : public BagInterface<ItemType>
{
    private:
            static const int DEFAULT_CAPACITY = 6; 
            ItemType items[DEFAULT_CAPACITY];      
            int itemCount;                          
            int maxItems;
    public:
          ArrayBag();
}

template<class ItemType>
ArrayBag<ItemType>::ArrayBag(): itemCount(0), maxItems(DEFAULT_CAPACITY)
{
}

and what's the difference between doing something like

template<class ItemType>
ArrayBag<ItemType>::ArrayBag()
{
     itemCount = 0;
     maxItems = DEFAULT_CAPACITY;
}

Is it just ease of access or is it more effective in processing power?

Thanks

  • 1
    The former is using the member initializer list syntax. See [cppref here](http://en.cppreference.com/w/cpp/language/initializer_list). – miradulo Apr 21 '18 at 00:26
  • 2
    This has nothing to do with templates. The same would apply to non-template class's constructors as well. When class members are simple plain types, like `int`, there is no practical difference. The situation is, of course, more complicated if your class members are complex classes in of themselves. – Sam Varshavchik Apr 21 '18 at 00:26

0 Answers0