I'm currently using something like that in my code:
class B : public A<C> { };
Wouldn't it be better to use a typedef?
typedef A<C> B;
The typedef keyword allows the programmer to create new names for types such as int or, more commonly in C++, templated types--it literally stands for "type definition". Typedefs can be used both to provide more clarity to your code and to make it easier to make changes to the underlying data types that you use.
Empty class: It is a class that does not contain any data members (e.g. int a, float b, char c, and string d, etc.) However, an empty class may contain member functions.
C++ allows creating an Empty class, yes! We can declare an empty class and its object. The declaration of Empty class and its object are same as normal class and object declaration.
Similarly, typedef can be used to define a structure, union, or C++ classC++ classThe C++ class is an extension of the C language structure. Because the only difference between a structure and a class is that structure members have public access by default and class members have private access by default, you can use the keywords class or struct to define equivalent classes.https://www.ibm.com › docs › SSLTBW_2.3.0 › cplr054Classes and structures (C++ only) - IBM.
It depends. If you want A<C>
and B
to be distinct but related types, B
should extend A<C>
. If you want them to be identical, you should use a typedef
. Can you provide any more context?
It depends on what you want. If you use your different classes to customize your template meta programming than it is better to create B
.
If you use inheritance just to reduce typing than it is not an appropriate solution and you have to go with typedef
, they were introduced for that.
Most definitely, if the intent is to give a new name to an existing datatype then use a typedef.
It is not exactly the same. For example with typedef you can do this:
typedef A<C> B;
func(B param) {...}
...
A<C> var;
func(var);
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