
11-29-2005
|
|
Registered User
|
|
|
Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,928
|
|
A short summary: - classes, and all the stuff that comes with them -- constructors, destructors, virtual functions, overloading, new/delete. This is chapters and chapters of stuff in itself, too complicated for a brief overview.
- global variable initialization from non-constants -- consider this use of a global variable:
Code:
#include <stdlib.h>
void *globalmem=malloc(1024);
int main()
{
return(0);
}
This program will not work in C because malloc(1024) is a function call, which is obviously not a constant value. But this will work in C++, which calls the appropriate functions before main() to take care of these strange global things.
- typecasting is stricter -- C will let you get away with assigning one kind of pointer to another -- or integer to pointer, or vice versa -- with only a warning, but in C++ it's a full-out error. You even need a typecast to convert from an enum to an integer in C++!
- calling an undeclared function is an error -- in C, undeclared functions are assumed to take integer paramaters, int somefunc(int param1, int param2); which sometimes works well enough, but not always. This is totally disallowed in C++. For example, this code "usually" works in C but not in C++:
Code:
/* #include <stdio.h> should be here, but we're too lazy! */
int main()
{
printf("Hello World\n");
return(1);
}
As it turns out, they had a good reason to disable this behavior in C++. C's "everything undeclared is an integer" assumption totally breaks on 64-bit systems where pointers are a different size than integers.
- hidden pointer madness -- C++ offers a syntax that I hate hate hate, that allows you to pass by reference without an obvious pointer. They call these "references". This C code:
Code:
void function(int *a, int b) { (*a) += b; }
is equivalent to this C++ code:
Code:
int function(int &a, int b) { a+=b; }
This syntax might fool you into thinking that you don't need to error-check pointers any more, but "int &a" is still a pointer, just a pointer that the compiler does tricks to let you use like a normal variable, and subject to segmentation faults if something feeds it a garbage value, or something that's been freed, or whatever -- just like real pointers.
- structures work in a more obvious way -- In C, you need to do this:
Code:
struct something { int val; };
struct something myvar;
In C++ you can do this:
Code:
struct something { int val; };
something myvar;
- function overloading -- In C, this won't work:
Code:
int value(int a, int b) { return(a+b); }
float value(float a, float b) { return(a+b); }
-- it'll complain that 'value' is being redeclared. This will work in C++ because it sneakily renames these functions behind the scenes so they're really NOT the same, and tries to be intelligent about which is called for what kind of paramaters.
This makes calling C functions from C++ -- and vice versa -- difficult. In both cases, C++ needs to be told to make the functions extern "C" so it doesn't mangle the names, like this:
Code:
extern "C" {
int function_called_from_c_code(int a, int b);
int function_called_by_c_code(int a, int b) { return(a+b); }
}
Last edited by Corona688; 11-29-2005 at 09:55 PM..
|