Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;继承重载具有不同参数的函数_C++_Polymorphism_Overloading_Using Declaration_Name Hiding - Fatal编程技术网

C++ C++;继承重载具有不同参数的函数

C++ C++;继承重载具有不同参数的函数,c++,polymorphism,overloading,using-declaration,name-hiding,C++,Polymorphism,Overloading,Using Declaration,Name Hiding,我正在做一个使用类继承的项目,需要在基类和派生类中使用大量重载,我已经简化了代码,但我不想不必要地复制和粘贴,因为继承就是为了这个目的 #include <iostream> class Base { public: Base() = default; //base const char* overload void foo(const char* message) { std::cout << message <

我正在做一个使用类继承的项目,需要在基类和派生类中使用大量重载,我已经简化了代码,但我不想不必要地复制和粘贴,因为继承就是为了这个目的

#include <iostream>

class Base
{
public:
    Base() = default;

    //base const char* overload
    void foo(const char* message)
    {
        std::cout << message << std::endl;
    }
    //other overloads ...
};
class Derived : public Base
{
public:
    Derived() = default;

    //derived int overload
    void foo(int number)
    {
        std::cout << number << std::endl;
    }
};

int main()
{
    Derived b;
    b.foo(10); //derived overload works
    b.foo("hi"); //causes error, acts as if not being inherited from Base class
    return 0;
}
#包括
阶级基础
{
公众:
Base()=默认值;
//基本常量字符*重载
void foo(常量字符*消息)
{

std::cout您可以在派生类中使用using声明,如

using Base::foo;
使基类中声明的重载函数foo在派生类中可见

这是插入using声明的程序

#include <iostream>

class Base
{
public:
    Base() = default;

    //base const char* overload
    void foo(const char* message)
    {
        std::cout << message << std::endl;
    }
    //other overloads ...
};
class Derived : public Base
{
public:
    Derived() = default;

    using Base::foo;
    //derived int overload
    void foo(int number)
    {
        std::cout << number << std::endl;
    }
};

int main()
{
    Derived b;
    b.foo(10); //derived overload works
    b.foo("hi"); //causes error, acts as if not being inherited from Base class
    return 0;
}

您可以在派生类中使用using声明,如

using Base::foo;
使基类中声明的重载函数foo在派生类中可见

这是插入using声明的程序

#include <iostream>

class Base
{
public:
    Base() = default;

    //base const char* overload
    void foo(const char* message)
    {
        std::cout << message << std::endl;
    }
    //other overloads ...
};
class Derived : public Base
{
public:
    Derived() = default;

    using Base::foo;
    //derived int overload
    void foo(int number)
    {
        std::cout << number << std::endl;
    }
};

int main()
{
    Derived b;
    b.foo(10); //derived overload works
    b.foo("hi"); //causes error, acts as if not being inherited from Base class
    return 0;
}

使用Base::foo;
添加到
Derived
的主体中。否则,只有
Derived::foo
重载可见。问题不是重载解析,而是名称查找。常见问题-不是Q或A的最清晰提炼,而是例如:/“为什么”此问题的版本:在
派生的
主体中添加
使用Base::foo;
。否则,只有
派生的::foo
重载可见。问题不是重载解析,而是名称查找。常见问题-不是Q或A的最清晰提炼,但例如:/“为什么”此问题的版本: