J
J
Julia2016-09-15 22:33:22
C++ / C#
Julia, 2016-09-15 22:33:22

What does ::value_type mean in the example?

Good evening. Tell me what it means in the example :: value_type and where can I get to know it better?
I so understood so it is possible to define own type.

template <typename T>       		 
class Point
{
public:
    typedef T value_type;

    T x,y;
};

int main()
{
    cout << "use of template classes" << endl;
  
    Point<int> P = {1,2};
    //Point<int>::value_type X = P.x;  /// ?????

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Moseychuk, 2016-09-16
@Julila

This is used in order to be able to get the template parameter from outside. Usually in order to have an abstract name for the type of stored elements, iterator types, and any types derived from template parameters.
It is this example that is unsuccessful, because value_typeyou take again from an explicitly specified template. If you change the type to , Pfor example Point<double>, then the type Xwill have to be changed.
Here's a better example:

typedef Point<int> MyPoint;

MyPoint P = {1, 2};
MyPoint::value_type X = P.x;

Now it is enough to change the type in one place, and not in every piece of code where it is used.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question