シングルトンクラスの実態宣言の場所について

このQ&Aのポイント
  • デザパタのシングルトンを勉強していたのですが、実態の宣言(Singleton* Singleton::theInstance = NULL;)はヘッダファイル内だとなぜだめなのでしょうか?
  • さらにprivate修飾がついてるのに、なぜNULLにセットできるでしょう。
  • シングルトンクラスの実態宣言の場所とNULLのセットについて
回答を見る
  • ベストアンサー

デザパタ シングルトンクラスの実態宣言の場所について

//-----singleton.h---------------------------------------- // Copyright(C) 2003 Yoshinori Oota All rights reserved. #ifndef SINGLTON #define SINGLTON #include <iostream> #include <string> using namespace std; class Singleton { public: static Singleton* Instance(); static void Destroy(); void nextState(); void printState() const; private: Singleton(); ~Singleton(); static Singleton* theInstance; string value_; }; //ここじゃだめなの? //Singleton* Singleton::theInstance = NULL; #endif //------------------------------------------------------- //-----singleton.cpp--------------------------------------- #include"singleton.h" Singleton* Singleton::theInstance = NULL; Singleton::Singleton() : value_("initial state") { } Singleton::~Singleton() { } Singleton* Singleton::Instance() { if (theInstance == NULL) { theInstance = new Singleton(); } return theInstance; } void Singleton::Destroy() { if (theInstance != NULL) { delete theInstance; theInstance = NULL; } } void Singleton::nextState() { if (value_ == "initial state") { value_ = "second state"; } else if (value_ == "second state") { value_ = "third state"; } else if (value_ == "third state") { value_ = "initial state"; } } void Singleton::printState() const { cout << value_ << endl; } //------------------------------------------------------ //----main.cpp------------------------------------------- #include"singleton.h" int main() { Singleton* theSingleton = Singleton::Instance(); theSingleton->printState(); theSingleton->nextState(); theSingleton->printState(); //デストラクタも隠蔽することにより、2重deleteを防げる Singleton::Destroy(); } //-------------------------------------------------------- デザパタのシングルトンを勉強していたのですが、 実態の宣言(Singleton* Singleton::theInstance = NULL;)はヘッダファイル内だとなぜだめなのでしょうか? さらにprivate修飾がついてるのに、なぜNULLにセットできるでしょう。 参照HP http://www002.upp.sonet.ne.jp/ys_oota/mdp/Singleton/index.htm

質問者が選んだベストアンサー

  • ベストアンサー
回答No.1

> 実態の宣言(Singleton* Singleton::theInstance = NULL;)はヘッダファイル内だとなぜだめなのでしょうか? ×実態 ○実体 それは宣言ではありません。定義です。 定義をヘッダに置くと、そのヘッダを#includeしたすべてのコンパイル単位に置いたことになり多重定義となります。 > さらにprivate修飾がついてるのに、なぜNULLにセットできるでしょう。 それができないと private なメンバは初期化不能となってしまいます。

関連するQ&A

  • C++ シングルトン マルチスレッド

    標準C++でシングルトンを実装したいのですが。 class Singleton{ public: static Singleton* getInstance(){ if (_instance == NULL){ //スレッドAがこの時点で、スレッドBがNULLチェックすると破綻する _instance = new Singleton(); } return _instance; } private: Singleton(); static Singleton* _instance; }; マルチスレッドになると上記のパターンで破綻するといわれどうしたものかと考えております。 static Singleton* _instance = new Singleton(); と出来れば解決なのですが 「static const int データメンバ以外をクラス内で初期化することはできません」 とのことでそれもできず。 どのようにすればよいでしょうか。

  • 構造体を宣言と共に初期化する方法

    構造体を宣言と共に初期化する方法はないのでしょうか? もしないのでしたら初期化用の関数を作ってそれを呼び出すしかないのでしょうか? ↓このような感じで ---kudamono.cppの内容 --- #include <stdio.h> struct{ static const char *name[5]; static const char *num[5]; }kudamono; void kuda(void){ kudamono.name[0] = "りんご"; kudamono.name[1] = "みかん"; kudamono.name[2] = "オレンジ"; kudamono.name[3] = "もも"; kudamono.name[4] = "キウイ"; kudamon.num[0] = "1個"; kudamon.num[1] = "3個"; kudamon.num[2] = "5個"; kudamon.num[3] = "2個"; kudamon.num[4] = "4個"; } --- main.cppの内容 --- #include <stdio.h> void kuda( void ); void main( void ){ printf( "%sが%sある" , kudamono.name[0] , kudamon.num[0] ); }

  • CakePHPでシングルトンパターン

    CakePHPでシングルトンパターンで、インスタンスを使いまわしたいのですが、 うまく動作しません。 以下のようなプログラムですが、何か設定が足りないのでしょうか class Test{   public static $instance = null;   public function __construct(){}   public function getInstance(){    if(is_null(Test::$instance)){     Test::$instance = new Test();    }    return Test::$instance;   } } 上記クラスを以下のように呼び出しております。 $instance = Test::getInstance(); 何度やっても、インスタンスが新しく生成されてしまします。 良い方法がございましたら、ご教授下さい。

    • ベストアンサー
    • PHP
  • c++ クラスに関してです。

    うまくクラス同士を連動させることができなくてエラーが出てしまいます。 どこが間違えているのかアドバイスくださると助かります。 #ifndef H22_H_ #define H22_H_ #include <string> #include "account.h" class Bank { public: Bank(); void deposit(double amount, const std::string& accountType); void withdraw(double amount, const std::string& accountType); void transfer(double amount, const std::string& accountType); double getBalance() const; double getSavingsBalance() const; double getCheckingBalance() const; private: Account checking, savings; }; #endif #ifndef ACCOUNT_H #define ACCOUNT_H #include <string> #include "account.h" class Account{ public: Account(); Account(double &amout); double deposit(double amount); double getBalance() const; double withdraw(double amount); double balance; private: }; #endif #include <iostream> #include "acount.h" using namespace std; Account::Account { balance = 0; } Account::Account(double amount) { balance = amount; } Account::double deposit() const { return balance; } Account::double getBalance(double balance) { balance += amount; return balance; } Account::double withdraw(double amount) { if(amount > balance) { balance -= 5; return balance; } elese { balance -= amount; return balance; }} h22.h の中身 #include <string> #include "h22.h" #include "account.h" #include <stdexcept> using namespace std; Bank::Bank() { Account checking; Account saving; } void Bank::deposit(double amount, const string& accountType) { if (accountType != "S" && accountType != "C") throw invalid_argument("Does not compute"); else if(accountType == "S") savings.balance += amount; else if(accountType == "C") checking.balance += amount; } void Bank::withdraw(double amount, const string& accountType) { if (accountType != "S" && accountType != "C") throw invalid_argument("Does not compute"); else if(accountType == "S") savings.balance -= amount; else if(accountType == "C") checking.balance -= amount; } void Bank::transfer(double amount, const string& accountType) { if (accountType != "S" && accountType != "C") { throw invalid_argument("Does not compute");} else if(accountType == "S") { savings.balance += checking.balance; savings.balance = 0;} else if(accountType == "C") { checking.balance += amount; checking.balance = 0;} } double Bank::getBalance() const { return getBalance(); } double Bank::getSavingsBalance() const { return savings.getBalance(); } double Bank::getCheckingBalance() const { return checking.getBalance(); } メイン.cpp http://pastebin.com/rQTj8xciです。 なにかヒントやアドバイスお願いします

  • 無名ネームスペース中のstatic宣言について

    お世話になります。 以下のように無名ネームスペース内でstatic定数を宣言することに 何か問題はあるでしょうか? QACという静的コードチェックツールで 「無名ネームスペース中のstatic宣言」と 警告が出てしまいます。 test.h ----------------------- class Test{ void testFunc(); }; test.cpp ------------------------ namespace { static const int i = 3; } void Test::testFunc(){ std::cout << i << std::endl; }

  • dlopenで目的の*.soファイルをロードできません

    以下C++のテストソース2ファイルをコンパイルし実行したのですが、libtest.soをロードすることが出来ません。OSはRed Hat Linux EL4.0WS、カーネルは2.6.9-5です。原因と思しき点がお分かりの方がいれば、教えて頂けますでしょうか。 ********** ソースファイル1(libtest.cpp) ********** #include <stdio.h> #include <stdlib.h> extern "C" {void printTest();} void printTest(){ printf(" *** SUCCESS!! *** Java - Native Interface test at INOAC\n"); } ********** ソースファイル2(test.cpp) ********** #include <dlfcn.h> #include <stdio.h> #include <stdlib.h> static void (*testFunc)(); int main(){ void *library; const char * error; library = dlopen("libtest.so",RTLD_LAZY); if(library==NULL){ printf("Could not open libtest.so\n"); exit(1); } dlerror(); testFunc = (void (*)())dlsym(library,"printTest"); error = dlerror(); if(error){ printf("Could not find the function printTest\n"); exit(1); } printf("Test from C;\n"); (*testFunc)(); dlclose(library); return 0; } ********** 実行方法 ********** pgCC -fPIC -shared libtest.cpp -o libtest.so pgCC -ldl test.cpp -o test.exe setenv LD_LIBRARY_PATH "." ./test.exe

  • C++ クラスの作り方

    Windows10の上で、Visual Studio Community2015 を使ってC++を勉強中の者です。 Time クラスというのを定義して使おうとしましたが、クライアント側のプログラム作成中にエラーメッセージが出てきて、色々考えましたがどこが悪いのかわかりません。 プログラム自体は時刻を設定してそれを表示させるだけのものです。 詳しい方がいらっしゃいましたら、ご教授お願い致します。 問題のエラーメッセージ; "consoleApplication69.cpp" を作成中にエディターの中で、波型の赤線が出ているところにカーソルを持っていくと、次の3か所でそれぞれ次のようなエラーメッセージが出てきます。 (1) #include "time3.h" のところ: 「ソースファイルをひらけません。"time3.h"」 (2) 関数 incrementMinute( )のプロトタイプのところ:「不完全な型は使用できません。」 (3) 最初のパラメーター Time & のところ:「識別子 Time が定義されていません。」 以下に、 1: 私が作ったTimeクラスの定義のあるヘッダー "time3.h", 2: メンバー関数の定義ファイル "time3.cpp" 3:クライアント側プログラム "consoleApplication69.cpp" の3つのソースコードをコピーします。 "time3.cpp" については長いので、上の半分だけにしました。 ****************************************************** 1: "time3.h" ******************************************************** #pragma once // header file for Time class // time3.h #ifndef TIME3_H #define TIME3_H class Time { public: Time(int = 0, int = 0, int = 0); ~Time(); void setTime(int, int, int); void setHour(int); void setMinute(int); void setSecond(int); int getHour(); int getMinute(); int getSecond(); void printUniversal(); void printStandard(); private: int hour; int minute; int second; }; #endif *********************************************** 2: "time3.cpp" ************************************************ #include <iostream> #include <iomanip> #include <cstdlib> #include <ctime> #include <cmath> #include <cstring> #include "time3.h" using namespace System; using namespace std; Time::Time(int hr, int min, int sec) { setTime(hr, min, sec); } void Time::setTime(int hr, int min, int sec) { setHour(hr); setMinute(min); setSecond(sec); } void Time::setHour(int hr) { hour = (0 <= hr && hr <= 23) ? hr : 0; } void Time::setMinute(int min) { minute = (0 <= min && min <= 59) ? min : 0; } void Time::setSecond(int sec) { second = (0 <= sec && sec <= 59) ? sec : 0; } ********************************************************* 3: "consoleApplication69.cpp" ********************************************************* // ConsoleApplication69.cpp : メイン プロジェクト ファイルです。 #include "stdafx.h" #include <iostream> #include <iomanip> #include "time3.h" using namespace System; using namespace std; //prototype void incrementMinute(Time &, const int); int main() { Time t; t.setHour(2); t.setMinute(10); t.setSecond(30); cout << " current time is "; cout << setfill('0'); cout << setw(2) << t.getHour() << ":"; cout << setw(2) << t.getMinute() << ":"; cout << setw(2) << t.getSecond() << "\n"; return 0; } // function void incrementMinute(Time &tt, const int num) { // まだ未定義 } またソリューションエクスプローラーの画面写真も添付します。 ヘッダーファイルもインクルードしていますし、ソリューションの中に入れたつもりですが、なぜ Time が定義されていないといわれるのでしょうか? どうぞよろしくお願いいたします。

  • extern記憶クラス指定子を使う事について

    prg1.cpp----------------------------- #include<stdio.h> void func(void); int gg=5678; int main(void) { printf("main gg=%d\n",gg); func(); return 0; } ---------------------------------- prg2.cpp-------------------------- #include<stdio.h> extern int gg; void func(void) { printf("func gg=%d\n",gg); } ---------------------------------- 以上「prg1.cpp」と「prg2.cpp」という名前のふたつのファイルを作成し、「prg1.cpp」で宣言したグローバル変数を「prg2.cpp」で利用可能にしたいと思っています。 それには、「prg1.cppをコンパイルし、prg2.cppもコンパイルして、両者のオブジェクト(コンパイル後のファイル)をリンクする」と参考書には書いてあったのですが、どのようにしたらリンクされるのかわかりません。 prg.1cpp---------------------- #include<stdio.h> void func(void); int gg=5678; int main(void) { printf("main gg=%d\n",gg); func(); return 0; } #include<stdio.h> extern int gg; void func(void) { printf("func gg=%d\n",gg); } ------------------------------- 以上のように、「prg1.cpp」のファイルに1つにまとめれば、なぜかよくわかりませんが実行できました。 しかし「prg1.cpp」と、「prg2.cpp」をリンクさせてみたいので、教えていただけると嬉しいです。

  • 別のファイルの値を得るには?

    ファイルを分割して関数を別のファイルにおいたのですが値を返してもらおうとしても帰ってきません どのようにしたら関数の値を得られますか? 大体このような感じですね ---main.cppの内容--- #include <stdio.h> #include "betu.h" void main( void ){ printf( "%d" , a ); } ---betu.hの内容--- static int a; void betu( void ); ---betu.cppの内容--- void betu( void ){ a = 5; }

  • 昇順ソート

    sort.txtから読み込んだ値を 昇順でソートして出力するにはどうしたらよいでしょうか? #include <stdio.h> #include <stdlib.h> #include <string.h> #include <search.h> /* 比較関数 */ int strcmp_asc(const void *, const void *); int main(void) { FILE *fin, *fout; int i; int length; char s[256], s2[256]; if( (fin=fopen("sort.txt","r"))==NULL) { printf("入力ファイルがオープンできません\n"); exit(EXIT_FAILURE); } if( (fout=fopen("file2.txt","w"))==NULL) { printf("出力ファイルがオープンできません\n"); exit(EXIT_FAILURE); } while(fgets(s, 256, fin) != NULL) { /* 要素数を求める */ length = sizeof(s) / 256; /* 昇順でソート */ qsort(s, length, 256, strcmp_asc); /* memset(s2, NULL, sizeof(s2)); for (i = 0; i < length; i++) { } */ fprintf(fout,"%s\n",s2); } fclose(fin); fclose(fout); return 0; } int strcmp_asc(const void *sa, const void *sb) { return strcmp((char *)sa, (char *)sb); } sort.txt 50 45 35 25 15 10 5 1 32 46 8 7 9 19 18 14 16 13 12 17 11 20 40 30 31 3 2 37 38 36 33 39 34 49 47 48 4 6 44 42 43 41 21 22 26 24 28 29 27 23

専門家に質問してみよう