gpt4 book ai didi

c++ - Effective C++ Item 43 了解如何访问模板化基类中的名称

转载 作者:行者123 更新时间:2023-11-30 04:26:37 26 4
gpt4 key购买 nike

在书的第 43 项中,据说下面的代码将无法编译。我的理解是,LoggingMsgSender::sendClearMsg方法实例化时会出现编译错误。

但是,对于三种编译器(VC 2005、gcc 4.4.1 和一种用于嵌入式设备)我已经尝试过了。它们都没有显示任何编译错误。

有没有编译器可以显示书中提到的错误?欢迎提出任何建议。感谢您的帮助。

(我在我的源代码中发现了 VC 2005 中与此类相关的潜在编译器错误模板基类函数调用,这就是我想要编译错误的原因。说来话长……)

class CompanyX
{
public:
void sendClearText(){};
};

typedef int MsgInfo;

template<typename Company>
class MsgSender {
public:
void sendClear(const MsgInfo&)
{
Company c;
c.sendClearText();
}
};


template<typename Company>
class LoggingMsgSender : public MsgSender<Company> {
public:
void sendClearMsg(const MsgInfo& info)
{
sendClear( info ); // ERROR : will not compile despite clearly being in base class.
}
};


int main()
{
LoggingMsgSender<CompanyX> sender;
sender.sendClearMsg(1); // specialization of the method!!!
}

最佳答案

http://liveworkspace.org/code/273c71cd53111dd8c6aaa54e64c53548例如。得到一个错误。在这种情况下,在函数 sendClearMsg 中我们应该使用 this->sendClear(info); 编译器是 gcc 4.7.1。那么,现在是 2012 年了,为什么还要使用旧的编译器?

关于c++ - Effective C++ Item 43 了解如何访问模板化基类中的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11480807/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com