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?
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.
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.
A typedef declaration lets you define your own identifiers that can be used in place of type specifiers such as int , float , and double .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With