Flat Leon Works

アプリやゲームを作ってます。

【C++】宣言と定義の早見表

宣言と定義の早見表

宣言だけの書き方は忘れがちなので早見表を作ってみました。

注意 : 一応コンパイルが通ることを確認していますが、もしかしたら間違った書き方をしているかもしれません。

宣言のみ定義(宣言を含む場合あり)備考
グローバル変数extern int a;int a;グローバルスコープ
static変数不可static int a;
ローカル変数不可int a;関数スコープ
関数int func( void );int func( void ){ return 0; }
static関数static int func( void );static int func( void ){ return 0; }定義のstaticは任意
inline関数inline int func( void );
int func2( void );
int func( void ){ return 0; }
inline int func2( void ){ return 0; }
inlineは宣言か定義どちらかにあればよい
関数テンプレートtemplate<class T>
int func( void );
template<class T>
int func( void ){ return 0; }
クラスclass A;class A{};struct、unionも同じ
メンバ変数不可class A {
int a;
};
staticメンバ変数class A {
static int a;
};
int A::a;
メンバ関数class A {
int func( void );
};
int A::func( void ){ return 0; }
仮想関数class A {
virtual int func( void );
};
int A::func( void ){ return 0; }
staticメンバ関数class A {
static int func( void );
};
int A::func( void ){ return 0; }
inlineメンバ関数class A {
inline int func( void );
int func2( void );
};
int A::func( void ){ return 0; }
inline int A::func2( void ){ return 0; }
inlineは宣言か定義どちらかにあればよい
メンバ関数テンプレートclass A {
template<class T>
int func( void );
};
template<class T>
int A::func( void ){ return 0; }
クラステンプレートtemplate<class T>
class A;
template<class T>
class A{};
メンバ関数テンプレート
(クラステンプレート内)
template<class T>
class A{
template<class U>
int func( void );
};
template<class T>
template<class U>
int A<T>::func( void ) { return 0; }
enum不可enum A{};

宣言のみのもの

宣言のみ定義備考
typedef宣言typedef int MyInt;なし
friend宣言 : クラスclass A {
friend class B;
};
なし
friend宣言 : クラステンプレートclass A {
template<class T>
friend class B;
};
なし
friend宣言 : 関数class A {
friend int func( void );
};
なし
friend宣言 : 関数テンプレートclass A {
template<class T>
friend int func( void );
};
なし
friend宣言 : メンバ関数class A {
friend int B::func( void );
};
なし
friend宣言 : メンバ関数テンプレートclass A {
template<class T>
friend int B::func( void );
};
なし
friend宣言 :
クラステンプレートのメンバ関数
class A {
template<class T>
friend int B::func( void );
};
なし
friend宣言 :
クラステンプレートのメンバ関数テンプレート
class A {
template<class T>
template<class U>
friend int B::func( void );
};
なしtemplate<class U>は
記述しなくてもコンパイル成功しました

Ideoneメモ : https://ideone.com/KZoMtG