Author | Message | Time |
---|---|---|
option | So I've been writing C++ code for a long time, yet there are some things I've realized recently (after a long, long fling with c) that I don't quite get. Why the hell is everything preceeded with underscores in C++?? I.e. ___int32 or something like that. What the hell is the point of that Also, anytime I've needed to cast something, I've now realized that I was doing it c-style? I.e. [code]struct Arg *arg = (struct Arg *) malloc(sizeof(struct Arg)); void *ptr; ptr = (struct Arg *) arg;[/code] Now that I'm coming back to C++ and considering using it for more advanced programming (worker threads, thread classes, etc.), I've seen a lot of that underscore shit and weird casts that I'm not familiar with, though I used (basic) C++ for quite some time. I.e. c++ casting: [code]const char *c = "text"; char *str = const_cast<char *> (c); cout << str << '\n';[/code] I feel like there's just something I'm not getting about C++, now that I've been using C for so long (because of college, I'm a junior and a CS major). However, when I took my second data structures course (which was a C++ course), I didn't ever see any of that underscore shit or super weird casting. Also, what the hell is a "typedef"? Thanks! | May 4, 2011, 7:24 PM |
MusicDemon | Maybe this will help you? | June 16, 2011, 3:42 PM |
Myndfyr | [quote author=option link=topic=18281.msg185189#msg185189 date=1304537046] Why the hell is everything preceeded with underscores in C++?? I.e. ___int32 or something like that. What the hell is the point of that [/quote] It indicates a non-standard keyword for the given compiler. "Everything" is not preceded with two underscores in C++. [quote author=option link=topic=18281.msg185189#msg185189 date=1304537046] Also, anytime I've needed to cast something, I've now realized that I was doing it c-style? I.e. [code]struct Arg *arg = (struct Arg *) malloc(sizeof(struct Arg)); void *ptr; ptr = (struct Arg *) arg;[/code] Now that I'm coming back to C++ and considering using it for more advanced programming (worker threads, thread classes, etc.), I've seen a lot of that underscore shit and weird casts that I'm not familiar with, though I used (basic) C++ for quite some time. I.e. c++ casting: [code]const char *c = "text"; char *str = const_cast<char *> (c); cout << str << '\n';[/code] [/quote] Well, you don't need to declare "c" as "const char*". You can declare it just as char*. You also don't need to cast away the const-ness of it in order to use it in a "cout << str" expression. You can do C style casts exactly like you showed, in C++. However, that doesn't necessarily apply to classes. Because classes have virtual functions, particularly when casting among variants of the chain, you need to be careful to use static_cast<T> or dynamic_cast<T>. [quote author=option link=topic=18281.msg185189#msg185189 date=1304537046]Also, what the hell is a "typedef"? [/quote] It creates a type alias. (Note that this isn't restricted to C++, typedefs are also part of C). If I say: [tt]typedef unsigned long long u64;[/tt] That indicates that the alias "u64" is an alias for "unsigned long long". You might wonder why this is a good thing instead of using #defines. (For instance, you can [tt]#define u64 unsigned long long[/tt], but that eliminates all relationships and boils it down to the lowest level). Some compilers allow you to use a "strict" option. For example: [code] typedef void* HANDLE; typedef HANDLE HWND; typedef HANDLE HBRUSH; [/code] Although both HWND and HBRUSH are typedef'd as HANDLE, and both are void*, some compilers allow you to opt in to making these types incompatible with each other. That is to say, you can't accidentally pass in an HBRUSH where an HWND would go without casting. It allows for simple type hierarchy to be established among primitive types | June 16, 2011, 4:18 PM |
BreW | [quote author=option link=topic=18281.msg185189#msg185189 date=1304537046] Also, anytime I've needed to cast something, I've now realized that I was doing it c-style? I.e. [code]struct Arg *arg = (struct Arg *) malloc(sizeof(struct Arg)); void *ptr; ptr = (struct Arg *) arg;[/code] [/quote] That's not C style. C implicitly casts void * types and C++ does not. In C++, you must explicitly cast the return of malloc, for example. This is the single language quirk that prevents C from being a true subset of C++. [quote author=option link=topic=18281.msg185189#msg185189 date=1304537046] [code]const char *c = "text"; char *str = const_cast<char *> (c); cout << str << '\n';[/code] [/quote] This is a bad example of casting. Couldn't you come up with anything else? Because of your novice status, I'm concerned you might actually try to cast immutable strings to char *, and end up having your program crash and burn at runtime instead of giving an informative error at compile time. Just don't do it. Always practice const correctness. Have you considered reading a book on C++ yet? All of these questions would have been answered and more. Asking on a forum should be reserved for problems a bit less trivial. | June 18, 2011, 12:40 PM |