cplusplus.com cplusplus.com
cplusplus.com   C++ : Forums : Beginners : using predefined classes
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Forums
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Articles
Lounge
Jobs

-

post  using predefined classes

hadi (3)
hi

should anyone please tell me what following line means ?


list <int>::size_type i;

thanks
|
helios (829)
I think I'll need some context. Where did you see this?
|
hadi (3)
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 ?
|
firedraco (657)
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.
|
Zhuge (102)
size_type is just a typedef of unsigned int, if I remember correctly.
|
Duoas (1458)
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.
|
hadi (3)
thanks a lot
now I understand
|

This topic is archived - New replies not allowed.
Home page | Privacy policy
© cplusplus.com, 2000-2008 - All rights reserved - v2.2
Spotted an error? contact us