Sign in to follow this  
ChiefArmstrong

references, statics

Recommended Posts

I've been reading the book "Beginning C++ Game Programming" (love it) and at the same time I'm taking a college course on Visual Basic. In Visual Basic (up to the point that I understand anyway), you have a Dim declared variable that exists only within its scope, you can declare a Static variable that will retain it's value, and if it's a global static you could change it's value from any procedure that has access to it. Now, I'm on Chapter 6 in the C++ Game book and learning about References, where if you pass a variable to a function from outside the function itself only recieves a copy of the variable, therefore whatever the function does to the variable only exists within the scope. But when you pass a variable to a funcion by Reference, the function has access to the actual variable itself, and what the function does to the variable it does to the actual value (if I understand this correctly). Statics in VB and References in C++, are they essentially the same thing or is there something else going on in the background I just haven't touched yet?

Share this post


Link to post
Share on other sites
When you pass by reference you are passing the memory address of the object (or variable). Since the function is given the memory address, any changes you make to the passed in variable/object will remain even after the function returns.

Share this post


Link to post
Share on other sites
C++ has static variables too, pretty much like VB, apparently.

References are essentially aliases ("another name") for other variables, so that when you modify the reference, you are in fact modifying the original variable. The variable has to exist outside of the function.

Share this post


Link to post
Share on other sites
References in C++ are akin to specifying Byref on a function's argument in VB.

I think C++ also has the static notion for local variables (variables declared within a method), but I don't remember what the syntax is right now. (maybe just static :) )

Be carefull though - I'm not sure about vb6, but vb7+'s local statics are per instance of the class, while I think that for C++ it's a single local static for all instances of the class.
(In VB if you create two object's of a class's type, and call a method that increments a static local variable, when you call the method on the other class instance it won't have incremented the value - each class maintains a different value, while in C++, the second class will see an increased value - see it this way - in vb static locals are basically class member variables that are only visible inside the method, in C++ they are like shared/static class members that are only visible inside the method.)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this