A question (c ++) related to the structure sequence of global variables)

Source: Internet
Author: User
Question:
# Include <iostream>
Int main ()
{
STD: cout <"this is in main" <STD: Endl;

Return 0;
}

How to make the output content of Mian () in the above Code as follows:
Start
This is in main
End

Answer:
The method I first came up with was to rewrite the cout class in STD and modify the implementation of the <operator. After interviewing my experts, it was dangerous to reload the operator, it is not realistic to rewrite all functions. At that time, I was frustrated and did not think of any other method.

When I came back, I found a word about global variables in my basic knowledge:
The global object is initialized before calling Main.
The above question can be fully realized by declaring a global variable.
The specific implementation code is as follows: add a GB class definition before main, and then create a global variable for this class:

# Include "stdafx. H"
# Include <iostream>

Class GB
{
Public:
GB: GB ()
{
STD: cout <"start" <STD: Endl;
};

GB ::~ GB ()
{
STD: cout <"end" <STD: Endl;
};
};

Gb;

Int main ()
{
STD: cout <"this is in main" <STD: Endl;

Return 0;
}

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.