using predefined classes

hi

should anyone please tell me what following line means ?


list <int>::size_type i;

thanks
I think I'll need some context. Where did you see this?
here it is :

int main( )
{
using namespace std;
list <int> c1;
list <int>::size_type i;

c1.push_back( 1 );
i = c1.size( );
cout << "List length is " << i << "." << endl;

c1.push_back( 2 );
i = c1.size( );
cout << "List length is now " << i << "." << endl;
}

I think I understood what it means
it means that since list is a template class the type of list size can differ with
different type parameters and type of the size in the class will be int in this case , is that correct ?
I believe
size_type
is a special variable that is used to store the
.size()
of
list <int>
. In answer to your question, they are creating a variable called i to store the .size() of c1.
size_type is just a typedef of unsigned int, if I remember correctly.
But not necessarily. But that is the general intent.

The list class looks something like this very simplified example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template <typename ValueType, ...>
class list
  {
  public:
    typedef unsigned long size_type;
    ...
    size_type size() const
      {
      return f_number_of_nodes;
      }
    ...
  private:
    size_type f_number_of_nodes;
    ...
  };

Now the actual type of "size_type" can be modified by changing just one line (line 5). Suppose five years from now the STL is working on my 128-bit computer. The number of items a list can hold are more than unsigned long, so the list will define "size_type" to match:
typedef unsigned tredint size_type;
(where "tredint" is our hypothetical 128-bit integer type")

And you can always reference that type through an actual type instance:
typedef list <int> ::size_type a_big_unsigned_number;

Hope this helps.
thanks a lot
now I understand
Topic archived. No new replies allowed.