two C++ questions...

From Visual Basic to GNU C, this is the place to talk programming.

Moderators: SecretSquirrel, just brew it!

two C++ questions...

Postposted on Sun Apr 11, 2004 6:11 pm

I've been programming C++ decently for a few years, but due to my....excrecable...education in it, recently picked up a few books, to learn how to do it right. (For example, my stupid teachers never once told me to use a pointer...or dynamic allocation...and, well, when in my projects I had to, you can guess how the learning curve went. Not well.)
I've been reading, among other things, some language-specifications type things....
2 things I don't quite get, even after reading them.
A. the C++ casts--const_cast,static_cast,dynamic_cast,reinterpret_cast. Huh? What's the deal? what are they, what's the difference between them and (type)expr casts?
B. References. I know perfectly well how to use functions with pass by reference, and so on...but I had never heard of using references outside of function parameters. What are they, how does one declare one, when are they used?
AMD Athlon 64 3000+, 1 gb DDR 400, Radeon x800.
endersdouble
Gerbil First Class
 
Posts: 136
Joined: Sun May 04, 2003 8:41 pm

Postposted on Mon Apr 12, 2004 6:40 am

endersdouble wrote:A. the C++ casts--const_cast,static_cast,dynamic_cast,reinterpret_cast. Huh? What's the deal? what are they, what's the difference between them and (type)expr casts?

They are more restricted versions of the general cast, and are intended to guard against programmer errors by forcing you to make your intentions more explicit. For example... const_cast only allows you to remove or add the const attribute; any attempt to change the underlying type is flagged as a compile-time error. I forget exactly what the other ones do, but the basic concept is similar, i.e. better compile-time checking.

I never use the new cast types myself... I just use the good ol' fashioned cast.

B. References. I know perfectly well how to use functions with pass by reference, and so on...but I had never heard of using references outside of function parameters. What are they, how does one declare one, when are they used?

I'd say that the second most common use of references is for return values of functions and operators. For example... the assignment operator always returns a reference to the left hand side of the assignment (the variable assigned to).

Declaring references "stand alone" is not particularly useful... if you do that, they are essentially just an alias for another variable.
(this space intentionally left blank)
just brew it!
Administrator
Gold subscriber
 
 
Posts: 37705
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Postposted on Tue Apr 13, 2004 4:31 pm

static_cast, const_cast, and reinterpret_cast provide the basic functionality of the C-style cast, while reducing the likelihood that you'll shoot yourself in the foot. Unlike jbi, I try to avoid C-style casts, although for numeric conversions I'll still do it for clarity.

I should (but don't) remember exactly what static_cast encompasses -- I know it will do the "safe" type conversions like double to int and certain types of class conversions.

const_cast, as jbi says, will add or remove the const attribute. Removing const is only guaranteed to work if the underlying object isn't actually const, although I don't think this tends to be an issue in practice (aside from the obvious problems of trying to write to something in read-only memory).

reinterpret_cast is a blanket "I know what I'm doing" to the compiler that instructs it to interpret the target object as though it were the specified type. The most likely legitimate use of reinterpret_cast is to convert pointer types (e.g. through void*) or convert between pointer type and int type.

The remaining cast type isn't something that you can do with a C-style cast, because it manages C++-specific features. dynamic_cast will cast pointers or references within a class hierarchy. When it does this, it will also check to make sure that the cast is valid (the dynamic type of the underlying object is compatible with the target type), and either return a null pointer (for a pointer type) or throw an exception (for a reference type) if the validation fails.


Incidentally, you shouldn't have all that many pointers or explicit dynamic allocation in good C++ code. References and containers generally are both better (requiring a lot less boilerplate) and safer (with less boilerplate, it's not as easy to omit something accidentally or through ignorance).
Craig P.
Gerbil Team Leader
 
Posts: 285
Joined: Tue Dec 31, 2002 3:12 am
Location: South Bend, IN

Postposted on Fri Apr 16, 2004 12:33 am

I think (on TR/forums) every one of us is a well educated person.
I also believe that most of us belong to a very well cultured society.
But when someone says
(For example, my stupid teachers never once told me to use a pointer...or dynamic allocation...and, well, when in my projects I had to, you can guess how the learning curve went. Not well.)

This is really not a healthy attitude toward teachers.
I am not saying that teachers must not be criticized, but their respect must be kept in mind while doing so.
Please always be +ve and do +ve criticism.
Thanks
knowledge has no limits so always be a student.
alimansoorahmad
Gerbil
 
Posts: 34
Joined: Fri Feb 06, 2004 2:46 am

Postposted on Sat Jun 12, 2004 8:24 pm

LOL, neither did my teacher. Still get confused with the difference between:

char *temp;
char temp[];
char temp[50];

;)
Windows XP - The 64-bit wannabe with a 32-bit graphics interface for 16-bit extensions to a 8-bit patch on a 4-bit operating system designed to run on a 2-bit processor by a company that can't stand 1-bit of competition
fc34
Minister of Gerbil Affairs
 
Posts: 2816
Joined: Wed May 08, 2002 8:39 am
Location: Somewhere

Postposted on Mon Jun 14, 2004 3:37 pm

Dynamic_cast is evaluated at run-time. All other casts are evaluated at compile-time. Being run-time, dynamic_cast will throw an exception (if casting a reference) or return a NULL (if casting a pointer), if your cast is invalid. Dynamic_cast requires run-time-type information to be enabled in the compile.

My take on reference/pointer: Always use references unless you can't (it's a generalization, but a good one for most cases). A reference is basically an "automatic" pointer -- i.e. the compiler keeps track of passing it around for you. The up-side to a reference is that it is always valid, never being "NULL", so you don't have to check it every time you pass it. (There is an exception to this, of course, which is deleting a referenced object while another variable still holds a reference to it; so don't do that.) Pointers require more care, to make sure they actually point to something valid whenever you move them around. But there are some things that can be done with pointers, which cannot be done (or at least can't be done easily) with references.
Buub
Maximum Gerbil
Silver subscriber
 
 
Posts: 4200
Joined: Sat Nov 09, 2002 11:59 pm
Location: Seattle, WA

Postposted on Mon Jun 14, 2004 3:56 pm

alimansoorahmad wrote:I think (on TR/forums) every one of us is a well educated person.
I also believe that most of us belong to a very well cultured society.
But when someone says
(For example, my stupid teachers never once told me to use a pointer...or dynamic allocation...and, well, when in my projects I had to, you can guess how the learning curve went. Not well.)

This is really not a healthy attitude toward teachers.
I am not saying that teachers must not be criticized, but their respect must be kept in mind while doing so.
Please always be +ve and do +ve criticism.
Thanks


Not true...I disagree. I loved most of my teachers, most of them were intelligent, and therefore, deserving of teaching their skill to the younger generation. But I had my fair share of stupid ones, and let me tell you, not only do stupid teachers not deserve respect, they should be ridiculed for their decision to pass their stupidity to those they are suposed to have dedicated their lives to enlightening. If you are not able to understand something yourself, why are you wasting my time trying to teach it to me?!? Its a complete waste of students time when their teachers are incompetent, or arent skilled enough to pass their knowledge to others. I took every opportunity (there werent many, thankfully) to correct my teachers when they made a mistake. Otherwise, theyll just F*** everyone else over with their incorrect knowledge. I am not a genius by any stretch, but come on...TEACH ME, and do it correctly, and youll EARN my respect.
Shuttle SN25P nForce4 & A64 4000 939 90nm
1024MB Corsair XMS PC3200 CAS2
ATI RADEON X850XT 256MB PCIE
Dual 74GB 10K WD RaptoRAID XP Pro SP2
JediNinjaWizards
Irritating Rash
 
Posts: 1627
Joined: Tue Aug 19, 2003 9:46 am
Location: Player's Republic of Pimpachusetts


Return to Developer's Den

Who is online

Users browsing this forum: No registered users and 4 guests