广告广告
  加入我的最爱 设为首页 风格修改
首页 首尾
 手机版   订阅   地图  繁体 
您是第 4941 个阅读者
 
发表文章 发表投票 回覆文章
  可列印版   加为IE收藏   收藏主题   上一主题 | 下一主题   
righer
数位造型
个人文章 个人相簿 个人日记 个人地图
初露锋芒
级别: 初露锋芒 该用户目前不上站
推文 x0 鲜花 x12
分享: 转寄此文章 Facebook Plurk Twitter 复制连结到剪贴簿 转换为繁体 转换为简体 载入图片
推文 x0
文章表情[C/C++] SCOPE的问题
想请教各位大大scope的问题
File Scope
Function scope
Local sc ..

访客只能看到部份内容,免费 加入会员 或由脸书 Google 可以看到全部内容



献花 x0 回到顶端 [楼 主] From:台湾教育部 | Posted:2005-10-14 09:57 |
youchun
数位造型
个人文章 个人相簿 个人日记 个人地图
小人物
级别: 小人物 该用户目前不上站
推文 x0 鲜花 x26
分享: 转寄此文章 Facebook Plurk Twitter 复制连结到剪贴簿 转换为繁体 转换为简体 载入图片

首先要先说明 scope 是要用来区分 visibility 和 lifespan
ANSI C 有四种
  Block
复制程式
{
    const int T_SIZE =100;
}
  File
复制程式
/* external linkage */
<foo1.c>
const int T_SIZE = 100;
<foo2.c>
extern const int T_SIZE;
  Function
复制程式
void foofunction(int t_size) { ... }
  Function prototype
复制程式
/* 亦是 declaration */
void foofunction(int);
C++ 除了已上还支援
(C++ Primer 3e 所提到)
  local
复制程式
/* 局限于函式定义内的一个局部区域 
 * 每一个函式都表现各自的一个 local scope
 * 可说是 block scope 与 function scope 的综合
*/
int size = 100;
void foo1(int size) {...}
int foo2(int size) { // local scope: level #1
        while(){ // local scope: level #2
            ...
        } 
        printf("test\n");; // local scope: level #1
}
  class
复制程式
/* 定义在 class 内部区域 */
class Foo {
public:
    int t_size;
    .
    .
    .
}
  namespace
复制程式
/* 超越于函式宣告, 函式定义或 class 定义
 * 以外的一个局部区域
 * 为了解决全域命名空间污染问题而生
*/
// ----------- uniqueFoo.h ------------
namespace uniqueFoo {
    class foo1 { ... }
    void foo2(int &);
    int foo3(int size) { ... }
    const int foobar = 100;
}

// ---------- foo.c ----------
#include "uniqueFoo.h"

void main() {
printf("foo = %d\n", uniqueFoo::foobar);
}

详细资料请网上找或阅读相关书籍
--
有错请不吝指正

此文章被评分,最近评分记录
财富:50 (by codeboy) | 理由: 感谢您的详细说明~^^


献花 x2 回到顶端 [1 楼] From:台湾中华电信 | Posted:2005-10-14 20:31 |
righer
数位造型
个人文章 个人相簿 个人日记 个人地图
初露锋芒
级别: 初露锋芒 该用户目前不上站
推文 x0 鲜花 x12
分享: 转寄此文章 Facebook Plurk Twitter 复制连结到剪贴簿 转换为繁体 转换为简体 载入图片

首先感谢youchun大大热情指导
不知道有没有大大能写出一个程式 里面包含
File Scope
Function scope
Local scope
Class scope
并且加以注解哪一段属于哪一种scope

因为以一个完整程式码加注解我比较容易看的懂
而我现在已经对这4个scope有一点了解
不过还不是很清楚 希望藉由一个简单又完整的程式
并写出哪一段属于哪一种scope
因为我目前想知道哪一段属于哪一种scope
所以再度麻烦各位大大为小弟解惑 谢谢


献花 x0 回到顶端 [2 楼] From:台湾教育部 | Posted:2005-10-15 05:46 |
youchun
数位造型
个人文章 个人相簿 个人日记 个人地图
小人物
级别: 小人物 该用户目前不上站
推文 x0 鲜花 x26
分享: 转寄此文章 Facebook Plurk Twitter 复制连结到剪贴簿 转换为繁体 转换为简体 载入图片

我觉得就 scope 来看变数的 visibility & lifespan
比区分哪个定义的 scope 有意义,
观念上能厘清即可
以下程式码有点杂乱
如果你可以了解 _number 是属于何种 scope
就 ok

复制程式
// -------------- test.h ---------------------
const int _number = 1000; // File scope

// -------------- test.c --------------------
// g++ compiles
#include <stdio.h>
#include "test.h"

extern const int _number; // File scope

class Foo { // beginning of class scope
public:
    Foo(int _number) {
        this->_number = _number;
    }
    int getnumber() {
        return this->_number;
    }
private:
    int _number;
}; // end of class scope

int fun(int _number) { // start of function scope & local scope
    return _number + 1;
} // end of function scope & local scope

int main(){
    Foo * foo = new Foo(_number); // external _number in test.h
    printf("foo->_number = %d at local level 1 \n", foo->getnumber()); // _number in class Foo
    int _number = 0; // _number at local scope level 1
    printf("_number = %d at level 1\n", _number);
    while(true) { // start of local scope level 2
        int _number = 3; // _number at local scope level 2
        _number = fun(_number);
        printf("_number = %d at level 2(while loop)\n", _number);
        for( int _number = 100; _number < 103; _number++) {
            printf("_number = %d at level 3(for loop)\n", _number); // local scope level 3
        }
        if (_number == 4) break;
    } // end of local scope level 2
        return 0;
}
输出如下:
foo->_number = 1000 at local level 1
_number = 0 at level 1
_number = 4 at level 2(while loop)
_number = 100 at level 3(for loop)
_number = 101 at level 3(for loop)
_number = 102 at level 3(for loop)

--
不完善的地方请大家指正


献花 x2 回到顶端 [3 楼] From:台湾中华电信 | Posted:2005-10-15 11:16 |

首页  发表文章 发表投票 回覆文章
Powered by PHPWind v1.3.6
Copyright © 2003-04 PHPWind
Processed in 0.023836 second(s),query:16 Gzip disabled
本站由 瀛睿律师事务所 担任常年法律顾问 | 免责声明 | 本网站已依台湾网站内容分级规定处理 | 连络我们 | 访客留言