Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
pointer vs non-pointer
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Your Name, post: 3525474"] "Scope" generally means "within the inner-most open/close braces." For example, in C++ you can even do things like: void func(int x, int z) { if (x == 5) { int y = x + 1; y *= z; } y += 14; // (Compile error because "y" is out of scope here!) } One obvious case is when you actually want to create certain things "dynamically." Suppose you're writing a game. When the user clicks the mouse button, you want to make a gun fire a bullet. You don't know at compile time how many times he might press that button. You would need to create a new "MyBullet" object every time he presses the button, and probably "delete" the objects when they hit something, or when they are far enough away that you can't see them any more. You can't do that in a clean way without creating the objects dynamically. A common trick in the old days of C (and even before) was to place a limit on the number of objects that could be created. That's the reason, when you play "Galaga," that you can't fire more than two bullets at a time. If you enforece a limit like this, then you can declare your objects statically: MyBullet bullets[2]; And then when the user presses the "fire" button, you have to check and make sure that at least one of the bullets has already scrolled off the top of the screen. If it has, you don't actually create anything, you just move the bullet that's hiding just off the screen back to the tip of the gun and start it going upward again. There are certainly benefits to static allocation - speed being a major one. There's also no possiblity of memory leaks. There are lots of reasons to use dynamic allocation, but it's never absolutely necessary. It just makes sense in a lot of cases. It's usually kind of a balancing act. Pat [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
pointer vs non-pointer
Top