关于C++ static 修饰符的考试题

请阅读如下代码(all.h,test1.cpp,test2.cpp,main.cpp),请问对main函数中的func1和func2来说,g_str是否是全局变量?对main中thread_group而言,g_str是否是全局变量?

两个小问题,欢迎参与哈:)

all.h

#ifndef ALL_H
#define	ALL_H

#include <iostream>

#include <boost/bind.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/thread/thread.hpp>

using namespace std;
using namespace boost;

static char g_str[] = "123456"; 
void fun1( );
void fun2( );

#endif	/* ALL_H */
test1.cpp

#include "all.h"

void print2() {
    //g_str[1]='P';   
    cout <<"func1: " <<g_str << "  ADDR: " << &g_str << endl;
}

void change12() {
    g_str[1] = 'm';
}

void change22() {
    g_str[1] = 'n';
}

void fun1() {
    //g_str[1]='P';
    print2();
    change12();
    print2();
    change22();
    print2();
}
test2.cpp

#include "all.h"

void print() {
    //g_str[1]='P';
    string tid=lexical_cast<string>(boost::this_thread::get_id());
    cout <<"func2: " <<tid<<" "<<g_str << "  ADDR: " << &g_str << endl;
}

void change1() {
    g_str[1] = 'x';
}

void change2() {
    g_str[1] = 'y';
}

void fun2() {
    //g_str[1]='P';
    print();
    change1();
    print();
    change2();
    print();
    cout<<"------"<<endl;
}

main.cpp

/* 
 * File:   main.cpp
 * Author: hcx
 *
 * Created on September 3, 2013, 9:03 PM
 */

#include <cstdlib>

#include "all.h"

//#include "t1.h"
//#include "t2.h"

int main(int argc, char** argv) {
    //--------------------------------------
    fun1(); 
    fun2(); 
    //--------------------------------------
    int num;
    thread_group threads;
    num = 4; 
    cout << "我是主程序,我准备产生" << num << "个子线程" << endl;
    for (int i = 0; i < num; i++) {
        threads.create_thread(bind(&fun2));
    }
    cout << "我是主程序,我在等子线程运行结束" << endl;
    threads.join_all();
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值