C++初心者です。
ほぼ触ったことがないので、細かい部分も教えていただきたいです。
C++を使用して試しにログを出力するプログラムを作成しています。
以下のサイトを参考にlog4Cppで作成しております。
http://qiita.com/hsagae/items/d7b0cf6ee876b636b3b9
以下のソースを実行して「Storm is coming」「This is Base」「This is Derived」
というログを出力させたいです。
理解が乏しく自身をもって言えてないですが、オーバーラードしているので、
問題ないと思っていたのですが、「This is Derived」のメッセージが出力されず、
「This is Base」が2回連続で出力されてしまいます。
オーバーライドの実装の仕方が間違えているのでしょうか。
どの箇所を直せばいいか修正箇所と修正サンプルを教えて
いただけないでしょうか。
以下ソースです。
■ConsoleApplication4.cpp
C++
1#include "stdafx.h" 2#include "Log.h" 3#include "Base.h" 4#include "Derived.h" 5 6int _tmain(int argc, _TCHAR* argv[]) 7{ 8 log4cpp::Category& logger = Log::getLogger(); 9 10 logger.warn("Storm is coming"); 11 12 Base* pBase = new Base(); 13 Base* pDerived = new Derived(); 14 15 pBase->printLog(); 16 pDerived->printLog(); 17 18 logger.shutdown(); 19 20 delete pDerived; 21 delete pBase; 22 23 return 0; 24}
■Log.h
C++
1#pragma once 2#include<log4cpp/Category.hh> 3#include<log4cpp/PropertyConfigurator.hh> 4 5class Log 6{ 7private: 8 Log(); 9public: 10 ~Log(); 11 static log4cpp::Category& getLogger(); 12};
■Log.cpp
C++
1#include "stdafx.h" 2#include "Log.h" 3Log::Log() 4{ 5} 6Log::~Log() 7{ 8} 9log4cpp::Category& Log::getLogger() 10{ 11 12 std::string initFileName = "log4cpp.properties"; 13 log4cpp::PropertyConfigurator::configure(initFileName); 14 return log4cpp::Category::getRoot(); 15}
■Base.h
C++
1#pragma once 2 3class Base 4{ 5public: 6 Base(); 7 ~Base(); 8 void printLog(); 9};
■Base.cpp
C++
1#include "stdafx.h" 2#include "Log.h" 3#include "Base.h" 4 5Base::Base() 6{ 7} 8 9Base::~Base() 10{ 11} 12 13void Base::printLog() 14{ 15 log4cpp::Category& logger = Log::getLogger(); 16 17 logger.info("This is Base"); 18}
■Derived.h
C++
1#pragma once 2#include "Base.h" 3 4class Derived : public Base 5{ 6public: 7 Derived(); 8 ~Derived(); 9 void printLog(); 10};
■Derived.cpp
C++
1#include "stdafx.h" 2#include "Log.h" 3#include "Derived.h" 4 5Derived::Derived() 6{ 7} 8 9 10Derived::~Derived() 11{ 12} 13 14void Derived::printLog() 15{ 16 log4cpp::Category& logger = Log::getLogger(); 17 18 logger.info("This is Derived"); 19}

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/01/06 08:17