07_vectors - CSCI-1200 Data Structures - Fall 2010 Lecture...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CSCI-1200 Data Structures — Fall 2010 Arrays and pointers Different types of memory (“automatic”, static, dynamic) Dynamic allocation of arrays 7.1 Today’s Lecture Designing our own container classes: Mimic the interface of standard library containers Study the design of memory management code and iterators. Move toward eventually designing our own, more sophisticated classes. Vector implementation Templated classes Copy constructors, assignment operators and destructors Optional Reading: Ford&Topp, Sections 5.3-5.5; 7.2 Vector Public Interface In creating our own version of the STL vector class, we will start by considering the public interface: public: // MEMBER FUNCTIONS AND OTHER OPERATORS T& operator[] (size_type i); const T& operator[] (size_type i); void push_back(const T& t); iterator erase(iterator p); void resize(size_type n, const T& fill_in_value = T()); void clear(); bool empty() const; size_type size() const; // ITERATOR OPERATIONS iterator begin(); const_iterator begin(); iterator end(); const_iterator end(); To implement our own generic (a.k.a. templated) vector class, we will implement all of these operations, manipulate the underlying representation, and discuss memory management. 7.3 Templated Class Declarations and Member Function Definitions In terms of the layout of the code in vec.h templated class . The keyword template and the template type name must appear before the class declaration: template <class T> class Vec Within the class declaration, T is used as a type and all member functions are said to be “templated over type T ”. In the actual text of the code files, templated member functions are often defined (written) inside the class declaration . The templated functions defined outside the template class declaration must be preceded by the phrase: template <class T> and then when Vec is referred to it must be as Vec<T> . For example, for member function create (two versions), we write: template <class T> void Vec<T>::create(. ..
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
7.4 Syntax and Compilation Templated classes and templated member functions are not created/compiled/instantiated until they are needed. Compilation of the class declaration is triggered by a line of the form: Vec<int> v1; with int replacing T . This also compiles the default constructor for Vec<int> because it is used here. Other member functions are not compiled unless they are used. When a different type is used with Vec , for example in the declaration: Vec<double> z; the template class declaration is compiled again, this time with double replacing T instead of int . Again, however, only the member functions used are compiled.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/21/2012 for the course CSCI 1200 taught by Professor Cutler during the Fall '08 term at Rensselaer Polytechnic Institute.

Page1 / 8

07_vectors - CSCI-1200 Data Structures - Fall 2010 Lecture...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online