我有一个类的层次结构,每个类都必须有一个特定的基类。比基类提供了张贴日志记录的功能,并可以获取日志通道的名称(基本上是使用日志的类的名称)。让我们将此类称为Logable
。
为了允许我的类多次从该Logable
类继承,我给它提供了一个模板参数,每个子代都将自身用作该参数。
实际上,我正在使用boost :: log库,但是用简单的LogableImpl
类简化了上述层次结构的示例,该类替换了boost :: log接收器。
#include <iostream>
#include <string>
// macro for logging in a boost::log style
#define LOG_DEBUG this->_loggerObj.logStream("debug")
#define LOG_INFO this->_loggerObj.logStream("info")
#define LOG_WARN this->_loggerObj.logStream("warning")
#define LOG_ERROR this->_loggerObj.logStream("error")
class LogableImpl
{
private:
std::string _channelName;
public:
LogableImpl(const std::string & channelName): _channelName(channelName) {}
std::ostream & logStream(const std::string & severetyLevel)
{
std::cout << _channelName << " " << severetyLevel;
return std::cout;
}
};
template <class Descendant>
class Logable
{
protected:
Logable(const std::string & channelName): _loggerObj(channelName) {}
LogableImpl _loggerObj;
};
class Base: private Logable<Base>
{
public:
Base()
: Logable<Base>("Base")
{}
void someMethod()
{
LOG_INFO << "some method is called" << std::endl;
LOG_ERROR << "an error happened" << std::endl;
}
};
class Derived: public Base, private Logable<Derived>
{
public:
Derived()
: Logable<Derived>("Derived")
{}
void someAnotherMethod()
{
LOG_INFO << "another method is called" << std::endl;
LOG_ERROR << "another error is happened" << std::endl;
}
};
int main()
{
Base b;
Derived d;
b.someMethod();
d.someMethod();
return 0;
}
显然,我使用MSVC 2008对该源进行编译尝试时出错
error C2385: ambiguous access of '_loggerObj'
1> could be the '_loggerObj' in base 'Logable<Base>'
1> or could be the '_loggerObj' in base 'Logable<Derived>'
1>d:\cpp\visualstudio\tests\workbench\test\main.cpp(55) : error C2248: 'Logable<Descendant>::_loggerObj' : cannot access inaccessible member declared in class 'Logable<Descendant>'
1> with
1> [
1> Descendant=Base
1> ]
1> d:\cpp\visualstudio\tests\workbench\test\main.cpp(29) : see declaration of 'Logable<Descendant>::_loggerObj'
1> with
1> [
1> Descendant=Base
1> ]
1>d:\cpp\visualstudio\tests\workbench\test\main.cpp(56) : error C2385: ambiguous access of '_loggerObj'
1> could be the '_loggerObj' in base 'Logable<Base>'
1> or could be the '_loggerObj' in base 'Logable<Derived>'
1>d:\prog\cpp\visualstudio\tests\workbench\boost_test\main.cpp(56) : error C2248: 'Logable<Descendant>::_loggerObj' : cannot access inaccessible member declared in class 'Logable<Descendant>'
1> with
1> [
1> Descendant=Base
1> ]
1> d:\cpp\visualstudio\tests\workbench\test\main.cpp(29) : see declaration of 'Logable<Descendant>::_loggerObj'
1> with
1> [
1> Descendant=Base
1> ]
1>Build log was saved at "file://d:\cpp\visualStudio\tests\workbench\test\Debug\BuildLog.htm"
1>boost_test - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
如何指定在
LOG_*
宏中使用的正确的基本成员?我觉得可以通过一些模板魔术来完成,但是无法弄清楚。
必须使用不支持C ++ 11x功能的MSVC2008完成
最佳答案
我通过使用c ++ 11明确指定应使用的Logable
来做到这一点。由于我们不知道this
的类型,因此我使用decltype
:
#define LOGABLE_TYPE typename std::remove_reference<decltype(*this)>::type
#define LOG_DEBUG this->Logable<LOGABLE_TYPE>::_loggerObj.logStream("debug")
#define LOG_INFO this->Logable<LOGABLE_TYPE>::_loggerObj.logStream("info")
#define LOG_WARN this->Logable<LOGABLE_TYPE>::_loggerObj.logStream("warning")
#define LOG_ERROR this->Logable<LOGABLE_TYPE>::_loggerObj.logStream("error")
在此处查看完整代码:http://ideone.com/1D5jrj