typedef?

WHat is the purpose of Typedef?

For instance, I'm trying to create a graph using Boost Graph Library.

In the following statement, why should I use typedef?

typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS> MyGraph;

Also, in C++ I'm guessing if we want to access a method in a class we use "::"? Is that correct?
that line creates an alias to the type boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS> called MyGraph, which is much easier to read
To access a method of a class object in C++ is used the dot object.method();, if it's a pointer an arrow pointer->method();, if it's a static function the scope operator classname::method();
Last edited on
Typedef's have two main reason: First, they provide nice shortcuts for otherwise very long type names. This is especially true when you start to use templates a lot (like the adjacency_list).

The second usage is, when you have to provide typedefs as part of classes. For example, if you want to write an own list-class instead of using the std::list. (There's no particular reason to do this - just an example). If you want your own list class to be able to work with some parts of the standard library, you have to define typedefs within your class, e.g. you have to have a type named "iterator" that is the return type of your mandatory function "begin()" and "end()".


Your question about the "::": as Bazzy said, it's one of the ways you use to address some parts of a class. You also have to use the :: when addressing these inner-typedef's I was speaking in my last paragraph. And you use them for addressing types within namespaces. For example, if you want an iterator to the standard-list, then you write: std::list<int>::iterator my_iterator; where "std" is the namespace, "list" is the name of the class template (<int> is the type parameter to the class. A bit complicated, just read over this if you don't get templates yet) and "iterator" is the inner typedef within the "list".

Hope it helped. ;)

Ciao, Imi.
Thanks Immi and Bazzy,

Yes, it really helped.

Topic archived. No new replies allowed.