Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences between typedef and using?

Tags:

c++

typedef

using

What are the differences between using

typedef Some::Nested::Namespace::TypeName TypeName;

or

using Some::Nested::Namespace::TypeName;

to provide the shorthand TypeName in the local scope?

like image 341
palm3D Avatar asked Oct 05 '11 06:10

palm3D


People also ask

What is difference between typedef and macro?

We can have symbolic names to datatypes using typedef but not to numbers etc. Whereas with a macro, we can represent 1 as ONE, 3.14 as PI and many more. We can have a type name and a variable name as same while using typedef. Compiler differentiates both.

What is the use of typedef?

typedef is a reserved keyword in the programming languages C and C++. It is used to create an additional name (alias) for another data type, but does not create a new type, except in the obscure case of a qualified typedef of an array type where the typedef qualifiers are transferred to the array element type.

What is the meaning of typedef?

A typedef declaration lets you define your own identifiers that can be used in place of type specifiers such as int , float , and double .

Why typedef better than definition?

The typedef has the advantage that it obeys the scope rules. That means you can use the same name for the different types in different scopes. It can have file scope or block scope in which declare. In other words, #define does not follow the scope rule.


1 Answers

typedef gives an alias name for the type.

typedef Some::Nested::Namespace::TypeName TypeName;

Once you do that, You can refer Some::Nested::Namespace::TypeName just by saying TypeName in the local namespace.


using declaration makes the type visible in the current namespace.

using Some::Nested::Namespace::TypeName;

Imports the type in the current namespace.

In this case, using the either of the above you can refer Some::Nested::Namespace::TypeName by just using TypeName in the local namespace.

like image 186
Alok Save Avatar answered Sep 28 '22 19:09

Alok Save