Differences between C and C++ – Completed
The keyword typedef
The keyword typedef is in C++ allowed, but no longer necessary when it is used as a prefix in union, struct or enum definitions. This is illustrated in the following example:
struct somestruct
{
int a;
double d;
char string[80];
};
When a struct, union or other compound type is defined, the tag of this type can be used as [...]
Continue Reading...
Differences between C and C++ Continued…
The usage of standard C functions
Normal C functions, e.g., which are compiled and collected in a run-time library, can also be used in C++ programs. Such functions however must be declared as C functions. As an example, the following code fragment declares a function xmalloc() which is a C function:
extern "C" void *xmalloc(unsigned [...]
Overloading ++ and –
Overloading the increment (and decrement) operator creates a slight problem: there are two version of each operator,
as they may be used as postfix operator (e.g., x++) or as prefix operator (e.g., ++x).
Suppose we define a class iterator whose members can be used to visit the elements of an array. The iterator object
will return a pointer [...]
The explicit keyword
Assume we have a class that’s doing all kinds of interesting stuff. Its public members could be, e.g.:
class Convertor
{
public:
Convertor();
Convertor(char const *str);
Convertor(Convertor const &other);
~Convertor();
operator char const*();
void anyOtherMemberFunction();
};
Objects of the class Convertor may be constructed using a default constructor and using a char const *. [...]
Continue Reading...
Overloading operator new(size_t)
If the operator new is overloaded, it must have a void * return type, and at least an argument of type size_t. The size_t
type is defined in stddef.h, which must therefore be included when the operator new is overloaded.
It is also possible to define multiple versions of the operator new, as long as each version [...]