C ++方法只有当对象转换为基类时可见吗? [英] C++ method only visible when object cast to base class?

查看:75
本文介绍了C ++方法只有当对象转换为基类时可见吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它必须是我的代码中的具体的,我不能发布。

基本上我有:

  class CParent 
{
public:
void doIt(int x);
};
class CChild:public CParent
{
public:
void doIt(int x,int y,int z);
};

CChild * pChild = ...
pChild-> doIt(123); // FAILS编译器,没有找到方法
CParent * pParent = pChild;
pParent-> doIt(123); // work fine

如何实现?



编辑:人们在谈论阴影/隐藏。但是doIt的两个版本具有不同数量的参数。当然不能混淆编译器,子类中的重载不能与父类版本混淆?可以吗?



我得到的编译器错误是:
错误C2660:'CChild :: doIt':function不接受1参数

解决方案

您已隐藏了一个方法。例如:

  struct base 
{
void method
void method(float);
};

struct derived:base
{
void method(int);
// base :: method(int)不可见。
// base :: method(float)不可见。
};

您可以用使用指令:

 类派生:public base 
{
using base :: method; //将所有的都放入。

void method(int);
// base :: method(int)不可见。
// base :: method(float)是可见的。
};






的参数,我会解决这个问题。 观察:

  struct base 
{
void method(int){}
};

struct derived:base
{
void method(int,int){}
//方法(int)不可见。
};

struct derived_fixed:base
{
使用base :: method;
void method(int,int){}
};

int main(void)
{
{
derived d;

d.method(1,2); //将编译
d.method(3); //将不会编译
}
{
derived_fixed d;

d.method(1,2); //将编译
d.method(3); //将编译
}
}

/ em>无论参数或返回类型如何都被遮蔽;它只是阴影的名称使用base ::< x> ;; 会带来基础的所有 ; x> 方法转换为可见性。


It must be something specific in my code, which I can't post. But maybe someone can suggest possible causes.

Basically I have:

class CParent
{
 public:
  void doIt(int x);
};
class CChild : public CParent
{
 public:
  void doIt(int x,int y,int z);
};

CChild *pChild = ...
pChild->doIt(123); //FAILS compiler, no method found
CParent *pParent = pChild;
pParent->doIt(123); //works fine

How on earth?

EDIT: people are talking about shadowing/hiding. But the two versions of doIt have different numbers of parameters. Surely that can't confuse the compiler, overloads in child class which can't possibly be confused with the parent class version? Can it?

The compiler error I get is: error C2660: 'CChild::doIt' : function does not take 1 argument

解决方案

You have shadowed a method. For example:

struct base
{
    void method(int);
    void method(float);
};

struct derived : base
{
    void method(int);
    // base::method(int) is not visible.
    // base::method(float) is not visible.
};

You can fix this with a using directive:

class derived : public base
{
    using base::method; // bring all of them in.

    void method(int);
    // base::method(int) is not visible.
    // base::method(float) is visible.
};


Since you seem insistent about the number of parameters, I'll address that. That doesn't change anything. Observe:

struct base
{
    void method(int){}
};

struct derived : base
{
    void method(int,int){}
    // method(int) is not visible.
};

struct derived_fixed : base
{
    using base::method;
    void method(int,int){}
};

int main(void)
{
    {
        derived d;

        d.method(1, 2); // will compile
        d.method(3); // will NOT compile
    }
    {
        derived_fixed d;

        d.method(1, 2); // will compile
        d.method(3); // will compile
    }
}

It will still be shadowed regardless of parameters or return types; it's simply the name that shadows. using base::<x>; will bring all of base's "<x>" methods into visibility.

这篇关于C ++方法只有当对象转换为基类时可见吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆