While there are some similarities between the structure of C and C++ programs, there are also some crucial differences.
C++ relies on an external standard library to provide assertion testing. The information the program needs to use this library
resides in the library file
assert macro tests a condition for correctness and terminates the
program if the test fails.
A C++ program consists of declarations that may be in different files.
Each function is on the external, or global, level and may not be declared in a nested manner.
The files act as modules and may be separately compiled. We will be discussing scope and storage classes in greater detail later in this course.
C++ was based on C and retains a great deal of the functionality. C++ does not retain complete source-level compatibility with C.
There are a few anomalies for C++ programmers trying to write C code, and C programmers trying to compile with a C++ compiler.
You cannot implicitly assign from a void* to any other type.
For instance, the following is perfectly valid in C (in fact, it's arguably the preferable way of doing it in C)
but it won't compile in C++.
The explanation from Bjarne Stroustrup himself is that this is not type safe.
What this means is that you can have a void* that points to anything at all, and if you then assign the address stored in that void*
to another pointer of a different type, there is not any warning at all about it.
Consider the following:
void *void_pointer = &an_int;
double *double_ptr = void_pointer;
*double_ptr = 5;
When you assign *double_ptr the value 5, it's writing 8 bytes of memory, but the integer variable an_int is only 4 bytes.
Forcing a cast from a void pointer makes the programmer pay attention to these things.