访问基类的方法与派生类的对象,该对象具有相同名称的方法。

时间:2023-01-15 20:55:50

when accessing foo() of "base" using derived class's object.

当使用派生类的对象访问“base”的foo()时。

#include <iostream>

class  base
{
      public:
      void foo()
     {
        std::cout<<"\nHello from foo\n";
     }
};

class derived : public base
{
     public:
     void foo(int k)
     {
        std::cout<<"\nHello from foo with value = "<<k<<"\n";
     }
};
int main()
{
      derived d;
      d.foo();//error:no matching for derived::foo()
      d.foo(10);

}

how to access base class method having a method of same name in derived class. the error generated has been shown. i apologize if i am not clear but i feel i have made myself clear as water. thanks in advance.

如何访问派生类中具有同名方法的基类方法。已显示生成的错误。如果我不清楚的话,我向你道歉,但我觉得我已经把自己说得一清二楚了。提前谢谢。

4 个解决方案

#1


10  

You could add using base::foo to your derived class:

可以使用base:::foo添加到派生类:

class derived : public base
{
public:
     using base::foo;
     void foo(int k)
     {
        std::cout<<"\nHello from foo with value = "<<k<<"\n";
     }
};

Edit: The answer for this question explains why your base::foo() isn't directly usable from derived without the using declaration.

编辑:这个问题的答案解释了为什么在没有使用声明的情况下,您的base::foo()不能直接从派生而来。

#2


6  

d.base::foo();

#3


2  

Add following statement to your derived class :

在派生类中添加以下语句:

using Base::foo ;

This should serve the purpose.

这应该能达到目的。

When you have a function in derived class which has the same name as one of the functions in base class, all the functions of base class are hidden and you have to explicitly bring them in scope of your derived class as mentioned.

当派生类中的函数与基类中的函数名称相同时,基类的所有函数都是隐藏的,必须显式地将它们引入到派生类的范围中。

#4


0  

we can call base class method using derived class object. as per below code.

我们可以使用派生类对象调用基类方法。按照下面的代码。

#include<iostream.h>
class base
{
public:
void fun()
{
    cout<<"base class\n";
}
};
class der : public base
{
public:
void fun()
{
cout<<"derived class\n";
}
};
int main()
{

der d1;


d1.base::fun();


return 0;
} 

Output will be base class.

输出将是基类。

#1


10  

You could add using base::foo to your derived class:

可以使用base:::foo添加到派生类:

class derived : public base
{
public:
     using base::foo;
     void foo(int k)
     {
        std::cout<<"\nHello from foo with value = "<<k<<"\n";
     }
};

Edit: The answer for this question explains why your base::foo() isn't directly usable from derived without the using declaration.

编辑:这个问题的答案解释了为什么在没有使用声明的情况下,您的base::foo()不能直接从派生而来。

#2


6  

d.base::foo();

#3


2  

Add following statement to your derived class :

在派生类中添加以下语句:

using Base::foo ;

This should serve the purpose.

这应该能达到目的。

When you have a function in derived class which has the same name as one of the functions in base class, all the functions of base class are hidden and you have to explicitly bring them in scope of your derived class as mentioned.

当派生类中的函数与基类中的函数名称相同时,基类的所有函数都是隐藏的,必须显式地将它们引入到派生类的范围中。

#4


0  

we can call base class method using derived class object. as per below code.

我们可以使用派生类对象调用基类方法。按照下面的代码。

#include<iostream.h>
class base
{
public:
void fun()
{
    cout<<"base class\n";
}
};
class der : public base
{
public:
void fun()
{
cout<<"derived class\n";
}
};
int main()
{

der d1;


d1.base::fun();


return 0;
} 

Output will be base class.

输出将是基类。