C ++ stack object and stack object

Source: Internet
Author: User

Stack objects are equivalent to local objects, and stack objects are equivalent to objects that can be shared with functions or statement blocks. Stack objects are automatically destroyed after the statement block is executed, and the heap objects need to be manually destroyed.

# Include <iostream> using namespace STD; struct object {//★★★★The constructor and type have the same name. No returned type object () {cout <"this is the constructor" <Endl;} // The Destructor name is the type name followed by a Tilde; there is no return value type. ~ Object () {STD: cout <"this is the Destructor! "<Endl ;}}; int main () {// defines an object, which is equivalent to calling the object's constructor O; {object O1; // use the stack object to test the destructor. The partial variable object * O2 = new object will be destroyed during the statement block structure; // define the heap object // Delete O2; // manually destroy the heap object} while (1 );}

Destroy heap object

# Include <iostream> using namespace STD; struct object {//★★★★The constructor and type have the same name. No returned type object () {cout <"this is the constructor" <Endl;} // The Destructor name is the type name followed by a Tilde; there is no return value type. ~ Object () {STD: cout <"this is the Destructor! "<Endl ;}; int main () {// destroy heap object * O1; {O1 = new object ;}delete O1; while (1 );}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.