本文介绍了指针返回问题的C ++协方差问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我的帖子含糊不清,因此我将重做.我正在从Visual Studio 6.0环境升级到Visual Studio 2012环境的项目中遇到此问题.

Since I had a lot of ambiguity in my post, I will redo it.This is a problem I am encountering in a project which I am upgrading from a visual studio 6.0 environment to a visual studio 2012 environment.

我有一个类是从followinh MFC类(CPropertyPage)派生的,该类包含以下功能.文件是afxdlgs.h(mfc类)

I have a class which is derived from the followinh mfc class (CPropertyPage) which contains the following function. file is afxdlgs.h (mfc class)

class CPropertyPage : public CDialog
{
public:
virtual CPropertySheet *GetParentSheet();
}

更改前向声明后,我似乎仍然遇到问题.这是由于我的配方不好造成的.因此,我已将其更改回原始形式.

I also still seem to have the problem after changing the forward declaration. Which was a result due to my bad formulation. So I have changed it back to it's original form.

派生类如下所示.标头

The derived class looks like this.header

class CBankDefImportSheet;
class CBankDefImportAssignPage : public CPropertyPage
{
protected:
    CBankDefImportSheet* GetParentSheet ();
}

在cpp中

#include "BankDefImportSheet.h"
CBankDefImportSheet* CBankDefImportAssignPage::GetParentSheet()
{
    return (CBankDefImportSheet *)GetParent ();
}

此外,CBangDefImportSheet是

furthermore the CBangDefImportSheet is

class CBankDefImportSheet : public CPropertySheet
{}

当我编译时,出现C2555错误,返回类型不同,并且与CPropertyPage :: GetParentSheet不协变.

when I compile I get the C2555 error that the return type differs and is not covariant from CPropertyPage::GetParentSheet.

我尝试添加CBankDefImportSheet的标头,但没有解决.我也读过一种可以在返回类型之后进行强制转换的可能性,但是不确定是否可以解决它,而且不确定在这种情况下该怎么做.

I have tried adding the header of CBankDefImportSheet but that did not solve it. I have also read a possibility of being able to cast after the return type, but unsure if that would solve it, furthermore unsure of how to do it in this case.

解决后,下面的帖子是问题的一部分,但是const正确性也是如此.真可惜!

After solving, post below was part of the problem, however, so was const correctness. Shame on me!

在标头中应指定为

 CBankDefImportSheet * GetParentSheet () const;

以及在cpp中

 CBankDefImportSheet * CBankDefImportAssignPage::GetParentSheet() const
 {
     return ((CBankDefImportSheet *)GetParent ());
 }

推荐答案

我在编辑中回答了自己的问题,但根据建议,我也会在此处添加它.我的问题的主要原因是mfc对话框函数是const.因此由于const正确性(或本例中的不正确性)而导致错误的协方差

I answered my own question in the edit, but following advice i will also add it here. The main cause of my problem was that mfc dialog functions are const. Thus resulting in wrong covariance because of const correctness (or incorrectness in this case)

CBankDefImportSheet* GetParentSheet ();

在标头中应指定为

CBankDefImportSheet * GetParentSheet () const;

以及在cpp中

CBankDefImportSheet * CBankDefImportAssignPage::GetParentSheet() const
{
return ((CBankDefImportSheet *)GetParent ());
}

我很高兴这已经帮助了至少另一个人.

I'm glad this helped at least one other person already.

这篇关于指针返回问题的C ++协方差问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 07:41