纯虚拟功能问题

时间:2020-08-24 17:25:07

标签: c++ oop polymorphism c++14 virtual-functions

我有一个基类,其中有一个纯虚函数,有了这个函数,我想在其他派生类中覆盖它(如果可能,在某些具有不同数量参数的类中)。

因此,在MergeSort子类中,我有MSort方法,因为它是递归完成的,因此需要不同数量的参数。

所以现在具有这些参数的功能让我收到此错误 'MergeSort:'无法实例化抽象类。但是,如果我从基类中覆盖Sort方法,效果很好,但是我不需要一个参数。

我还尝试声明另一个具有不同数量参数的虚函数,并在MergeSort类中对其进行定义,我得到了相同的东西。

我还想澄清一下,我还有其他子类用于不同的算法(气泡排序,插入排序等),这些子类的实现与MergeSort(构造函数和排序函数)相似,但排序函数具有相同的参数个数(仅用于图形界面),就像上面的基类一样。

那么可以使用具有不同数量参数的覆盖方法吗?还是我上面所说的其他解决方案?

// BASE CLASS
// Forward declaration
class Interface;

/**
 * Base class from which the sorting algorithms classes will inherit (Polymorphic class) 
 * The base class will allow us to create a sequence with n elements    
 */
class SortingAlgorithms
{

protected:
    std::vector<sf::RectangleShape> sequence;       // vector which will contain a randomized sequence
    std::vector<sf::RectangleShape> sequenceCpy;    // a copy of sequence used for interaction features
    sf::RenderWindow& window;                       // initializes the window
    int minimum, maximum;                           // the range in which the elements will be randomized
    int elements;                                   // the number of elements which will be initialized

public:
    SortingAlgorithms();

    /** SortingAlgorithms() - class constructor which initializes the sequence
     *  @param min - the minimum value for randomizing
     *  @param max - the maximum value for randomizing
     *  @param els - the number of elements to generate
     *  @param win - since the window will be initialized only once (singleton pattern); 
     *               it will be needed to pass on this object to almost every function that has 
                     graphics features
     */
     SortingAlgorithms(int min, int max, int els, sf::RenderWindow& win);


     // A pure virtual function for overriding and param init which is what I described about win param from SortingAlgorithms constructor 
     virtual void Sort(std::unique_ptr<Interface>& init) = 0;
};



class MergeSort : public SortingAlgorithms
{
public:
    MergeSort(int min, int max, int els, sf::RenderWindow& win);
    
    void Merge(std::unique_ptr<Interface>& init, int first, int mid, int last);
    void MSort(std::unique_ptr<Interface>& init, int first, int last);
};

2 个答案:

答案 0 :(得分:5)

如注释中所述,您必须对所有替代使用相同的签名。为此,您可以使用以下方法: 使用重写函数作为一种入口点,在其中您可以调用执行排序的真实函数(可能是私有函数)。一个例子来说明这种方法:

class SortingAlgo
{
public:
    virtual void sort(int arr[], int n) = 0;
};

class BubbleSort: public SortingAlgo
{
public:
    void sort(int arr[], int n){
        this->bubble_sort(arr, n);
    }
private:
    void bubble_sort(int arr[], int n){
        //implemetation
    }
};

class MergeSort: public SortingAlgo
{
public:
    void sort(int arr[], int n){
        this->mergeSort(arr, 0, n - 1);
    }
private:
    void mergeSort(int arr[], int l, int r){
        //recursive implemetation
    }
    void merge(int arr[], int l, int m, int r){
        //implemenation
    }
};

答案 1 :(得分:-1)

这是解决具有不同参数量的虚方法重载问题的另一种方法。只要extern int x; 是同一类型,就可以使用。如果类型不同,则可能必须应用parameter-types并使用templates或使用template-type deduction ...

这是示例代码和使用variadic function templates编译的程序集,可以在Compiler Explorer上看到。

CPP

GCC 10.2

ASM

class Base {
public:
    virtual void foo(int a, int b, int c, int d) = 0;
};

class Bar : public Base {
public:
    // Requires All 4 Argumements - Don't Deafult any Parameter
    virtual void foo(int a, int b, int c, int d) override {
        // Use every parameter for this version of the function
    }
};

class Baz : public Base {
public:
    // Requires 3 Arguments - Default the Last Parameter
    virtual void foo(int a, int b, int c, int d = 0) override {
        // Use the first three parameters within this version of the function. 
    }
};

class Fiz : public Base {
public:
    // Requires 2 Arguments - Default the Last 2 Parameters
    virtual void foo(int a, int b = 0, int c = 0, int d = 0) override {
        // Use the first two parameters within this version of the function.   
    }
};

class Buz : public Base {
public:
    // Requires Only 1 Agument - Default All but the 1st Parameter
    virtual void foo(int a, int b = 0, int c = 0, int d = 0) override {
        // Use only the first parameter within this version of the function.
    }
};


int main() {
    Bar bar;
    bar.foo(1,2,3,4);
    Baz baz;
    baz.foo(5,6,7);
    Fiz fiz;
    fiz.foo(8,9);
    Buz buz;
    buz.foo(10);

    return 0;
} 

正如我已经说过的,当函数声明定义中每个参数-参数放置的类型是相同类型时,这将在简单情况下起作用。这里的技巧是获取具有最大数量参数的函数版本,并将其用于抽象基类内的纯虚函数的声明中。

现在,如果类型不同,您仍然可以执行此操作,但是它们的位置必须匹配,例如:

Bar::foo(int, int, int, int):
        push    rbp
        mov     rbp, rsp
        mov     QWORD PTR [rbp-8], rdi
        mov     DWORD PTR [rbp-12], esi
        mov     DWORD PTR [rbp-16], edx
        mov     DWORD PTR [rbp-20], ecx
        mov     DWORD PTR [rbp-24], r8d
        nop
        pop     rbp
        ret
Baz::foo(int, int, int, int):
        push    rbp
        mov     rbp, rsp
        mov     QWORD PTR [rbp-8], rdi
        mov     DWORD PTR [rbp-12], esi
        mov     DWORD PTR [rbp-16], edx
        mov     DWORD PTR [rbp-20], ecx
        mov     DWORD PTR [rbp-24], r8d
        nop
        pop     rbp
        ret
Fiz::foo(int, int, int, int):
        push    rbp
        mov     rbp, rsp
        mov     QWORD PTR [rbp-8], rdi
        mov     DWORD PTR [rbp-12], esi
        mov     DWORD PTR [rbp-16], edx
        mov     DWORD PTR [rbp-20], ecx
        mov     DWORD PTR [rbp-24], r8d
        nop
        pop     rbp
        ret
Buz::foo(int, int, int, int):
        push    rbp
        mov     rbp, rsp
        mov     QWORD PTR [rbp-8], rdi
        mov     DWORD PTR [rbp-12], esi
        mov     DWORD PTR [rbp-16], edx
        mov     DWORD PTR [rbp-20], ecx
        mov     DWORD PTR [rbp-24], r8d
        nop
        pop     rbp
        ret
main:
        push    rbp
        mov     rbp, rsp
        sub     rsp, 32
        mov     eax, OFFSET FLAT:vtable for Bar+16
        mov     QWORD PTR [rbp-8], rax
        lea     rax, [rbp-8]
        mov     r8d, 4
        mov     ecx, 3
        mov     edx, 2
        mov     esi, 1
        mov     rdi, rax
        call    Bar::foo(int, int, int, int)
        mov     eax, OFFSET FLAT:vtable for Baz+16
        mov     QWORD PTR [rbp-16], rax
        lea     rax, [rbp-16]
        mov     r8d, 0
        mov     ecx, 7
        mov     edx, 6
        mov     esi, 5
        mov     rdi, rax
        call    Baz::foo(int, int, int, int)
        mov     eax, OFFSET FLAT:vtable for Fiz+16
        mov     QWORD PTR [rbp-24], rax
        lea     rax, [rbp-24]
        mov     r8d, 0
        mov     ecx, 0
        mov     edx, 9
        mov     esi, 8
        mov     rdi, rax
        call    Fiz::foo(int, int, int, int)
        mov     eax, OFFSET FLAT:vtable for Buz+16
        mov     QWORD PTR [rbp-32], rax
        lea     rax, [rbp-32]
        mov     r8d, 0
        mov     ecx, 0
        mov     edx, 0
        mov     esi, 10
        mov     rdi, rax
        call    Buz::foo(int, int, int, int)
        mov     eax, 0
        leave
        ret
vtable for Buz:
        .quad   0
        .quad   typeinfo for Buz
        .quad   Buz::foo(int, int, int, int)
vtable for Fiz:
        .quad   0
        .quad   typeinfo for Fiz
        .quad   Fiz::foo(int, int, int, int)
vtable for Baz:
        .quad   0
        .quad   typeinfo for Baz
        .quad   Baz::foo(int, int, int, int)
vtable for Bar:
        .quad   0
        .quad   typeinfo for Bar
        .quad   Bar::foo(int, int, int, int)
typeinfo for Buz:
        .quad   vtable for __cxxabiv1::__si_class_type_info+16
        .quad   typeinfo name for Buz
        .quad   typeinfo for Base
typeinfo name for Buz:
        .string "3Buz"
typeinfo for Fiz:
        .quad   vtable for __cxxabiv1::__si_class_type_info+16
        .quad   typeinfo name for Fiz
        .quad   typeinfo for Base
typeinfo name for Fiz:
        .string "3Fiz"
typeinfo for Baz:
        .quad   vtable for __cxxabiv1::__si_class_type_info+16
        .quad   typeinfo name for Baz
        .quad   typeinfo for Base
typeinfo name for Baz:
        .string "3Baz"
typeinfo for Bar:
        .quad   vtable for __cxxabiv1::__si_class_type_info+16
        .quad   typeinfo name for Bar
        .quad   typeinfo for Base
typeinfo name for Bar:
        .string "3Bar"
typeinfo for Base:
        .quad   vtable for __cxxabiv1::__class_type_info+16
        .quad   typeinfo name for Base
typeinfo name for Base:
        .string "4Base"

这也应该起作用,但我尚未对其进行测试。