トップ 差分 一覧 ping ソース 検索 ヘルプ PDF RSS ログイン

C++ 外部記憶クラス(extern)



目次



記事一覧

キーワード

C++ 外部記憶クラス(extern)

[C++][C++によるオブジェクト指向プログラミング]


情報をブロックや関数の間で伝達する1つの方法

  • 外部変数を使用する
  • 変数が関数の外部で宣言されると、恒久的に記憶領域がそれに割り当てられる
  • その場合の記憶クラスのキーワードはextern

circle.cpp
double PI = 3.14159;

double circle(double radius)
{
    return (PI * radius * radius);
}

oop01.cpp
// extern を指定するとコンパイラは変数がどこか他の場所か
// 他のファイルで宣言されていると判断する
extern double PI;

// 関数は自動的にextern
double circle(double);

int main()
{
    double x = 3.5;
    
    cout << PI << endl;

    cout << circle(x) << endl;
}



YAGI Hiroto (piroto@a-net.email.ne.jp)
twitter http://twitter.com/pppiroto

Copyright© 矢木 浩人 All Rights Reserved.