Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.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++_Polymorphism_Overloading - Fatal编程技术网

C++ 使用派生类重载函数

C++ 使用派生类重载函数,c++,polymorphism,overloading,C++,Polymorphism,Overloading,我想做的是这样的: void SomeFunction(Base *){//do something} void SomeFunction(Derived *){//do something else} 这是C++中的可能吗?到目前为止,我的尝试只是调用函数的基本版本。 这里有一个例子来说明问题 示例: #include <iostream> #include <vector> class Base { public: Base () {std

我想做的是这样的:

void SomeFunction(Base *){//do something}
void SomeFunction(Derived *){//do something else}
<>这是C++中的可能吗?到目前为止,我的尝试只是调用函数的基本版本。 这里有一个例子来说明问题

示例:

#include <iostream>
#include <vector>

class Base
{
    public:
        Base () {std::cout << "Base Constructor for " << this << std::endl;}
        void virtual PrintSomething ()
        {
            std::cout << "I am Base!" << std::endl;
        };
};

class Derived : public Base
{
    public:
        Derived () : Base () {std::cout << "Derived Construtor for " << this << std::endl;}
        void virtual PrintSomething ()
        {
            std::cout << "I am Derived!" << std::endl;
        };
};

void DoAmazingStuff ( Base * ) {std::cout << "Amazing!" << std::endl;}
void DoAmazingStuff ( Derived * ) {std::cout << "DERP!" << std::endl;}

int main ()
{
    std::vector<Base *> some_vector;

    Base *x = new Base ();
    Derived *y = new Derived ();

    some_vector.push_back ( x );
    some_vector.push_back ( y );

// This is the part that isn't functioning as expected.
/******************************************/
    DoAmazingStuff ( some_vector[0] );
    DoAmazingStuff ( some_vector[1] );
/******************************************/

    std::cin.get ();
    std::cin.get ();
    delete some_vector[0];
    delete some_vector[1];
}
#包括
#包括
阶级基础
{
公众:

Base(){std::cout当然有可能;但是只有指针的静态类型为
派生*
时才会调用
派生版本。例如:

Base * b = new Base;        // static and dynamic types are Base
Derived * d = new Derived;  // static and dynamic types are Derived
Base * bd = new Derived;    // static type Base, dynamic type Derived

SomeFunction(b);   // Base overload
SomeFunction(d);   // Derived overload
SomeFunction(bd);  // Base overload (from static type)
动态分派(根据其动态类型选择函数)仅在调用虚拟成员函数时发生,而不是重载非成员函数:

struct Base {
    virtual void SomeFunction() {/*do something*/}
};
struct Derived : Base {
    virtual void SomeFunction() {/*do something else*/}
};

b->SomeFunction();  // Base version
d->SomeFunction();  // Derived override
bd->SomeFunction(); // Derived override (from dynamic type)
在成员函数的帮助下,您可以在非成员函数上实现类似于动态分派的功能:

void SomeFunction(Base * b) {b->SomeFunction();}

如评论中所述,此技术可以扩展为实现多分派,根据多个函数参数的动态类型选择函数。但这远远超出了问题的范围。

您能否向我们展示一个如何使用这些函数的示例?您是否可以做一个?您的目标是什么?Put
SomeFunction
作为虚拟函数在类上添加。添加,抱歉,花了一些时间!@PeterWood这不是我想要实现的。+1.还值得一提的是,如果在这种情况下需要动态调度,则搜索的术语是“双重调度”或“访问者模式”
void SomeFunction(Base * b) {b->SomeFunction();}