变量及对象的作用域

代码

#include 
<iostream>
using namespace std;

class Scope
{
public:
    Scope(
const char* infor);
    
~Scope();
private:
    
char information[40];
};

Scope::Scope(
const char* infor)
{
    strncpy_s(information,infor,
40);
    cout 
<< "Constructor called for " << information << endl;
}

Scope::
~Scope()
{
    cout 
<< "Destructor called for " << information << endl;
}

void function()
{
    Scope localScopeInFunction(
"LocalScopeInFunction");
    
static Scope StaticScopeInFunction("StaticScopeInFunction");
    Scope 
*scope = new Scope("ScopeInFunctionOnHeap");
    cout 
<< "In function()" << endl;
}

Scope GlobalScope(
"GlobalScope");

void main_scope()
{
    Scope scopeInMain(
"ScopeInmain");
    Scope 
*scope = new Scope("ScopeInMainOnHeap");
    cout 
<< "In Main before call function()" << endl;
    function();
    cout 
<<"In Main after call  function()." << endl;
}



原文地址:https://www.cnblogs.com/flaaash/p/1895122.html