My New C++ Endeavors

Awesome news! I've been asked at work to start doing a bit of GUI development for our NASA projects. They've said that all of their stuff is developed using Borland C++. Now with my PennMUSH development experience, I've taught myself a bit of C++ (though my PennMUSH development experience has been mostly debugging and making sure it compiles under Windows, so it isn't too in depth).

One of the big hangups I've always had with C++ is the pointer and reference concepts. And I think I know why now. It's because immediately after teaching us pointers in college, our teacher jumped right into recursion. And it was hard enough grasping pointers without sending my mind into an out-of-control fractal spiral.

However, reading C++ All-in-One for Dummies (the big 7-volume one that I got at Barnes and Noble for a surprising $35), the entire concept of pointers suddenly made sense to me!!

Here it is in a nutshell:
Variable has a value.
Pointer has a value also. Its value is actually a hex word.
Pointer's value, that hex word, points at the memory location of the variable.
We can access the variable's value via the pointer, which expands our capabilities of manipulating it.

And in short:
var = var value
&var = var location
ptr = var location
*ptr = var value

As you can see, all we REALLY care about in the end is what or where the variable is.

Then we get into heap variables where we don't even care what the variable is called! We only work with it via pointers!

Then apparently C++ has this new thing that wasn't in C called Referencing. This is, at least from what I know, a shorthand way of passing actual variables in and out of functions.

Functions normally just pass variable values in and out:
int manipulate(int input) {
    bigValue = input + 5;
    return bigValue;
}
int main() {
    smallValue = 5;
    cout << smallValue << endl; // Will return 5.
    nextValue = manipulate(smallValue);
    cout << nextValue << endl; // Will return 10 (Function added 5 to 5).
    cout << smallValue << endl; // Will return 5. Actual variable passed in is unchanged, because we really only passed in its VALUE.
}
With references, we can actually change the VARIABLE we pass in:

void manipulate(int &smallValue) {
    smallValue += 5;
    // We aren't returning anything! We don't have to because we're not manipulating an internal function variable anymore.
}
int main()  {
    smallValue = 5;
    cout << smallValue << endl; // Will return 5
    manipulate(smallValue);
    cout << smallValue << endl; // Will return 10. The REFERENCE to the variable was passed in, rather than the value, so the function worked on the variable we were referencing rather than one it created itself.
}
So I'm not too adept on the actual advantages to all this, but then again, I don't need to know its advantages right away in order to get excited about understanding it!


No comments :

Post a Comment

Comment loud, comment often. But comment on the content!

All comments are filtered through to my email, so your spam will never make it. Unless, of course, you wanted to try injection attacks into my email, which would probably not happen since Blogger just tells me that a comment is awaiting moderation and doesn't bother to tell me what it says. I trust Blogger like that..