C++プログラミング入門(4) // C++文法について

【テンプレート】


#include
using namespace std;

// Type型のメンバ変数をもつクラスA
template  class A{

private:
 const Type data;

public:

 A(){
  cout << "default constructor\n";
 }


 A(Type d):data(d){ // 初期設定なので代入
  cout << "constructor\n";
 }

 /*
 A(Type d){
  data = d; // constなメンバに代入はできない
  cout << "constructor\n";
 }
 */

 ~A(){
  cout << "destructor\n";
 }

 Type getData(){
  return data;
 }
};

int main(){

 A  hoge_float(1);
 A  hoge_int(3.3);
 A  hoge_double(3.3);

 cout << "hoge_float:" << hoge_float.getData() << endl;
 cout << "hoge_int:" << hoge_int.getData() << endl;
 cout << "hoge_double:" << hoge_double.getData() << endl;

 return 0;
}

コメント

このブログの人気の投稿

Callback関数を知らん人がまず理解すべきことのまとめ。

C言語でBluetoothスタックを叩きたい人のBluetooth開発入門その1

C++プログラミング入門(1) // 倉庫番プログラムの実装