Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ empty class or typedef

Tags:

c++

class

typedef

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;
like image 331
gregseth Avatar asked Feb 01 '10 13:02

gregseth


People also ask

When should I use typedef?

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.

What is the use of empty class?

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.

Can a class be empty?

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.

Can you typedef a class?

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.


4 Answers

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?

like image 190
Jon Purdy Avatar answered Nov 15 '22 04:11

Jon Purdy


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.

like image 27
Mykola Golubyev Avatar answered Nov 15 '22 04:11

Mykola Golubyev


Most definitely, if the intent is to give a new name to an existing datatype then use a typedef.

like image 33
JRL Avatar answered Nov 15 '22 06:11

JRL


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);
like image 20
tony.ganchev Avatar answered Nov 15 '22 05:11

tony.ganchev