Let me give a shout out to my friend Jason who has kindly taken the time to write up some C++ code to help get my point across. I think he can do it without any help from me.
I’m sure it goes without saying that C++ has a lot of similarities to Java. For example, it has a keyword, typedef, which is basically a way of declaring a variable that is automatically type-safe as well as having the same semantics as a typedef. I think this helps to make C++ code less verbose and more readable.
However, C (and Java) can be a bit of a pain to learn. There are a lot of pitfalls that can come up and we certainly don’t want to use them in our code. For example, if you’re using a built-in function in your code and it’s giving you a hard time, you can always use the compiler’s “smart” keyword to tell the compiler to stop.
Compilers generally give you a warning when you use the smart keyword, but the compiler will stop the code if you use it when compiling your code. If for example, you defined a variable as int x = 10, then you can use the smart keyword to stop it from being compiled, and have the compiler not compile your code. However, you can still have an int variable declared as a typedef, and then use the smart keyword, but you don’t get the same behavior.
But a good compiler knows when to use the smart keyword, and when to use the typedef. However, this doesn’t always work.
It’s often a good idea to use the typedef when possible. The typedef is very useful for when you don’t know the type of the variable, as it allows you to compile your code without knowing the type.
The best example of this is the c++ dictionary example, in which the compiler could fail to compile the code, but you still have the typedef to tell it the type. This is because the compiler is using the typedef to determine which declaration to use. However when you use the typedef, the compiler will still compile the code, but will fail to compile the typedef declaration.
c++ has the typedef keyword, and you can use it in a declaration statement, but you can’t declare it as a type. The compiler is smart enough to know when to use the typedef statement, but it doesn’t know when to use the type declaration statement.
C++ is a really, really, really, big language, but because the compiler is smart enough to know when to use the typedef statement, it still compiles it, but fails to compile the type declaration statement. I can’t tell if this is a bug or I’m doing something wrong.
Yes c++ is a big language. But the point is, in C++, if you use the typedef statement, you can now declare your own typedef type, but you cant use it without the type declaration statement.