0

I'm taking a course where we're being introduced to a few software patterns, one of them being the iterator pattern, and we're being asked to implement it in C++. Our professor gave us some sample code a few weeks ago and I'd like to understand it a little better than I do. First I'll post the code:

template <class T_,std::size_t SIZE_>
class carray {
public:
typedef T_              value_type;
typedef std::size_t     size_type;
typedef T_ &            reference;
typedef T_ const &      const_reference;
typedef T_ *            pointer;
typedef T_ const *      const_pointer;
typedef T_ *            iterator;
typedef T_ const *      const_iterator;
typedef std::ptrdiff_t  difference_type;

typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;

// data store
value_type data[SIZE_];

size_type   size() const { return SIZE_; }
reference operator [] ( size_type idx ) { return data[idx]; }
const_reference operator [] ( size_type idx ) const { return data[idx]; }

reverse_iterator    rbegin() { return reverse_iterator( end() ); }
reverse_iterator    rend() { return reverse_iterator( begin() ); }
const_reverse_iterator  rbegin() const { return const_reverse_iterator( end() ); }
const_reverse_iterator  rend() const { return const_reverse_iterator( begin() ); }
const_reverse_iterator  crbegin() const { return const_reverse_iterator( end() ); }
const_reverse_iterator  crend() const { return const_reverse_iterator( begin() ); }

iterator    begin() { return data; }
iterator    end() { return data + SIZE_; }
const_iterator  begin() const { return data; }
const_iterator  end() const { return data + SIZE_; }
const_iterator  cbegin() const { return data; }
const_iterator  cend() const { return data + SIZE_; }

};

In this template our prof defined a bunch of typedefs, and a few of them had both a non-constant and constant version. First, I wonder what the purpose of each of the public members are, and second, I wonder what the purpose of the constant versions are, in the context of defining a data structure template.

I also notice that some of the member functions are defined as 'const' and I wonder what the purpose of that is.

mcraenich
  • 745
  • 1
  • 14
  • 38

3 Answers3

1

I wonder what the purpose of each of the public members are.

The class contains the following:

  • Type aliases: Type information that is exposed from the class for public use.

    • The non-const typedefs are type aliases for a non-const T.
    • The const typedefs are type aliases for a const T.

    These typedefs are useful because the user of the class might need more information about the type. By using the typedef exposed by the class, he can obtain this. Here's a good example - Consider the situation where we have a template type and we need to access the underlying iterator type. By using the exposed type alias, we can access it:

    template<class Container>
    void example(Container& c)
    {
        typename Container::iterator begin = c.begin(),
        //       ^^^^^^^^^^^^^^^^^^^
                                     end   = c.end();
    }
    

    I wonder what the purpose of the constant versions are.

    Sometimes the user needs to use const types. Therefore using the type information that class provides makes this simple.

I also notice that some of the member functions are defined as 'const' and I wonder what the purpose of that is.

const member functions are functions that do not modify any of the classes data members.

David G
  • 94,763
  • 41
  • 167
  • 253
  • Thanks. In this example, I wonder why the need for a constant iterator at all? Why do we have two begin functions returning a const and non-const iterator? – mcraenich Feb 18 '14 at 02:36
  • @CanadianCoder We never know the user's intentions. Therefore we provide them with both options. For example, if the user wants to use a `const` `carray` object, then they need `const` iterators if they wish to iterate over it. They can't use the non-`const` versions to do that. – David G Feb 18 '14 at 02:43
1
  1. all the typedefs aim to facilitate the use of these types, std::size_t std::ptrdiff_t is just unsigned int typedefs. chech here for reverse_iterator

  2. const varialbe defined for the return value of the const version function, which is provided for the const obj, check here

  3. member function defined as const make sure the member variable is not changed in the function body, check here

Community
  • 1
  • 1
michaeltang
  • 2,850
  • 15
  • 18
1
  1. For this, I think you should try to use this template, and you may really know why it need such public members.

  2. while we are using this template to operate on some data structure, if we want to modify some data, we need a non-constant version. On the other side, if we want to change the value of data, we need a constant version which can help us protect data.

  3. Constant functions can help you, while you are trying to operate on some data and you do not want to modify the member data.

sunnyleevip
  • 165
  • 1
  • 14