警告 C4407 可能会导致什么问题?
我通过多重继承在某些 MFC CWnd 派生对象上使用纯虚拟接口时收到了一些警告。我相信这是由定义消息映射需要实现的方法引起的。
warning C4407: cast between different pointer to member representations, compiler may generate incorrect code
这听起来不仅仅是一个警告,更像是可能导致堆损坏的东西。那么是否有另一种方法可以执行类似于下面的操作,并且不会导致 MFC 动态向下转换宏比平常更加阻塞?
class ISomeInterface
{
public:
virtual LRESULT OnSomeRegisteredMessage(WPARAM wp, LPARAM lp) = 0;
};
class CSomeCoolWnd : public CWnd, public ISomeInterface
{
public:
LRESULT OnSomeRegisteredMessage(WPARAM wp, LPARAM lp);
};
BEGIN_MESSAGE_MAP(CSomeCoolWnd , CWnd)
ON_REGISTERED_MESSAGE(WM_USER_DEFINED, &CSomeCoolWnd::OnSomeRegisteredMessage)
END_MESSAGE_MAP()
我唯一想到的就是从接口中注释掉消息处理程序,并留下注释告诉消费者他们应该实现它们。然而,最好通过编译器错误来强制执行,而不是让他们使用接口并在运行时因缺少的东西而获得意外的结果。
I got some warnings by using pure virtual interfaces on some MFC CWnd
derived objects through multiple inheritance. I believe it's caused by defining the methods which need to be implemented for the message map.
warning C4407: cast between different pointer to member representations, compiler may generate incorrect code
That sounds like a bit more than a warning, more like something that might cause heap corruption. So is there another way to do something similar to below that won't cause the MFC dynamic downcast macros to choke anymore than usual?
class ISomeInterface
{
public:
virtual LRESULT OnSomeRegisteredMessage(WPARAM wp, LPARAM lp) = 0;
};
class CSomeCoolWnd : public CWnd, public ISomeInterface
{
public:
LRESULT OnSomeRegisteredMessage(WPARAM wp, LPARAM lp);
};
BEGIN_MESSAGE_MAP(CSomeCoolWnd , CWnd)
ON_REGISTERED_MESSAGE(WM_USER_DEFINED, &CSomeCoolWnd::OnSomeRegisteredMessage)
END_MESSAGE_MAP()
The only thing I've come up with is commenting out the message handlers from the interfaces and leaving comments telling the consumer that they should implement them. However it would be nice to enforce that through a compiler error rather than letting them use an interface and get unexpected results at runtime from things being missing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关于指向成员值的指针的不同表示形式的精彩描述可以在文章 Member 中找到函数指针和最快的 C++ 委托。本质上,所有不同的继承类型都可能需要使用不同的成员函数指针表示形式。这是特定于编译器的,本文讨论了许多不同的编译器(截至本文撰写时的 2005 年)。
显然,使用虚函数的多重继承可能需要与简单的成员函数指针不同的表示形式。
ON_REGISTERED_MESSAGE()
中的某个位置可能有一个在您发布的代码中不可见的强制转换。An excellent description of the different representations of pointer-to-member values can be found at the article Member Function Pointers and the Fastest Possible C++ Delegates. Essentially, all the different inheritance types can require the use of different member function pointer representations. This is compiler-specific and the article talks about a number of different compilers (up to 2005 when the article was written).
Evidently your use of multiple inheritance with virtual functions may require a different representation than a simple pointer-to-member function. There's probably a cast somewhere in
ON_REGISTERED_MESSAGE()
that isn't visible in the code you posted.尝试使用这样的东西:
Try to use something like this: