11.2 new Keyword


11.2 new Keyword

To create a variable dynamically at run time, the new keyword is used. This creates a variable without a sense of scope, so no matter where it is used to create a new variable, the variable will continue to exist until the program ends or until it is manually destroyed by the delete keyword (as discussed later). The following code creates an integer variable, and this variable is given global scope insofar as it will not be deleted when the function ends. It will not be deleted from memory until the program instructs it to be deleted. The new keyword returns a pointer to the newly created object.

      #include <iostream>      int main()      {         int *Number = new int;         *Number = 5;         std::cout<<*Number;            return 0;      } 




Introduction to Game Programming with C++
Introduction to Game Programming with C++ (Wordware Game Developers Library)
ISBN: 1598220322
EAN: 2147483647
Year: 2007
Pages: 225
Authors: Alan Thorn

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net