• Welcome to Valhalla Legends Archive.
 

Some noobie C++ questions (type-casting, etc) - typedef

Started by option, May 04, 2011, 02:24 PM

Previous topic - Next topic

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.

struct Arg *arg = (struct Arg *) malloc(sizeof(struct Arg));

void *ptr;

ptr = (struct Arg *) arg;


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:
const char *c = "text";
char *str = const_cast<char *> (c);
cout << str << '\n';


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!
option's BNET Development Blog
Current project: Fully-modular 100% C++ SCBW ChatBot
Current Task: Write the Packet Debuffer
New to BNET development like myself? Read and learn.
http://bnetdev.tech-vault.net/

MusicDemon

Official representative for PvPGN.

MyndFyre

Quote from: option on May 04, 2011, 02:24 PM
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
It indicates a non-standard keyword for the given compiler.  "Everything" is not preceded with two underscores in C++. 

Quote from: option on May 04, 2011, 02:24 PM
Also, anytime I've needed to cast something, I've now realized that I was doing it c-style? I.e.
struct Arg *arg = (struct Arg *) malloc(sizeof(struct Arg));
void *ptr;
ptr = (struct Arg *) arg;


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:
const char *c = "text";
char *str = const_cast<char *> (c);
cout << str << '\n';

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 from: option on May 04, 2011, 02:24 PMAlso, what the hell is a "typedef"?
It creates a type alias.  (Note that this isn't restricted to C++, typedefs are also part of C).  If I say:
typedef unsigned long long u64;
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 #define u64 unsigned long long, but that eliminates all relationships and boils it down to the lowest level).  Some compilers allow you to use a "strict" option.  For example:


typedef void* HANDLE;
typedef HANDLE HWND;
typedef HANDLE HBRUSH;


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
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

brew

Quote from: option on May 04, 2011, 02:24 PM
Also, anytime I've needed to cast something, I've now realized that I was doing it c-style? I.e.
struct Arg *arg = (struct Arg *) malloc(sizeof(struct Arg));
void *ptr;
ptr = (struct Arg *) arg;

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 from: option on May 04, 2011, 02:24 PM
const char *c = "text";
char *str = const_cast<char *> (c);
cout << str << '\n';

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.
<3 Zorm
Quote[01:08:05 AM] <@Zorm> haha, me get pussy? don't kid yourself quik
Scio te esse, sed quid sumne? :P