Quantcast
Channel: When should I use typedef in C++? - Stack Overflow
Viewing all articles
Browse latest Browse all 14

Answer by Imran Al Noor for When should I use typedef in C++?

$
0
0

There is one another use case to use typedef is when we want to enable a kind of Container Independent code (but not exactly!)

Let us say you have class:

Class CustomerList{public:    //some functionprivate:    typedef list<Customer> CustomerContainer;    typedef CustomerContainer::iterator Cciterator;};

The above code encapsulates the internal container implementation using typedef and even if in future the list container needs to changed to vector or deque still the user of the CustomerList class doesn't need to worry about exact container implementation.

Hence, the typedef encapsulates and somewhat help us to write Container Independent code


Viewing all articles
Browse latest Browse all 14

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>