简体   繁体   English

我可以从我的超类中调用子类构造函数吗?

[英]Can I call the subclass constructor from my superclass?

I want to know if I can return a subclass object through an overloaded operator from my superclass. 我想知道是否可以通过超类的重载运算符返回子类对象。

#include <stdio.h>
#include <iostream>
using namespace std;

struct AndSpecification;

struct Specification{
    virtual bool is_satisfied(int i) = 0;  



    AndSpecification operator&& (Specification & other){
        return AndSpecification(*this, other);
    }

};


struct Specification1 : Specification{
    int k;
    Specification1(int k) : k(k){}

    bool is_satisfied(int i){
        if (i > k)
            return true;
        return false;
    }

};

struct Specification2 : Specification{
    int k;
    Specification2(int k) : k(k){}

    bool is_satisfied(int i){
        if (i < k)
            return true;
        return false;
    }
};

struct AndSpecification : Specification{
    Specification& first;
    Specification& second;

    AndSpecification(Specification& first, Specification& second) : first(first), second(second){}

    bool is_satisfied(int i) override{
        return first.is_satisfied(i) && second.is_satisfied(i);
    }
};

I think that the result is that I can't use the constructor of my subclass because it is not yet defined. 我认为结果是我无法使用子类的构造函数,因为尚未定义它。 The error messages are: 错误消息是:

main.cpp: In member function 'AndSpecification Specification::operator&&(Specification&)': main.cpp:在成员函数'AndSpecification Specification :: operator &&(Specification&)'中:

main.cpp:20:56: error: return type 'struct AndSpecification' is incomplete AndSpecification operator&& (Specification & other){ ^ main.cpp:20:56:错误:返回类型'struct AndSpecification'不完整AndSpecification运算符&&(Specification&other){^

main.cpp:21:45: error: invalid use of incomplete type 'struct AndSpecification' return AndSpecification(*this, other); main.cpp:21:45:错误:无效使用不完整类型'struct AndSpecification'返回AndSpecification(* this,other);

Your incomplete forward class declaration cannot be used in this manner until the class is fully defined. 在完全定义类之前,不能以这种方式使用不完整的前向类声明。 Incomplete (forward) class declaration can be used in certain cases, but this is not one of them. 在某些情况下可以使用不完整的(转发)类声明,但这不是其中一种。

A C++ compiler reads the source in order, from start to finish. C ++编译器从头到尾依次读取源代码。 When it sees your operator, it has no idea what this mysterious class is, that it's returning. 当看到您的操作员时,不知道这个神秘的类是什么,它正在返回。 It has not been defined yet. 尚未定义。 It only gets defined later, in the header/source file. 它仅稍后在头文件/源文件中定义。

You need to declare the class method, and then define it only after the class it's returning is fully defined: 您需要声明类方法,然后仅在完全返回的类定义好之后再定义它:

// ...

struct Specification{
    virtual bool is_satisfied(int i) = 0;  



    AndSpecification operator&& (Specification & other);

};

// After AndSpecification is declared, later:

inline AndSpecification Specification::operator&& (Specification & other){
    return AndSpecification(*this, other);
}

As an alternative to inline , put the definition of the operator method into one of the translation units. 作为inline方法的替代方法,将运算符方法的定义放入一个转换单元中。

Whenever the compiler must know the size of a type, it must be defined. 每当编译器必须知道类型的大小时,都必须对其进行定义。 A declaration is not enough for constructing a type. 声明不足以构造类型。

In your case, the simple fix is to make operator&& a free function and move it to the bottom: 在您的情况下,简单的解决方法是使operator &&为自由函数并将其移至底部:

AndSpecification operator&& (Specification & left, Specification & right){
        return AndSpecification(left, r)ight;
    }

In my opinion, free binary operators are better than member functions. 在我看来,自由二进制运算符要好于成员函数。

You've implemented an inline function definition before completing the definition of one of the classes you're using in the function. 在完成对函数中使用的类之一的定义之前,您已经实现了内联函数定义。 The compiler knows there's a struct AndSpecification but it doesn't know that the particular constructor you're using exists. 编译器知道存在一个struct AndSpecification但不知道您使用的特定构造函数存在。 Declare your method in the class but don't define it until after the definition of AndSpecification . 在类中声明您的方法,但要等到AndSpecification定义之后再定义它。

struct Specification{
    virtual bool is_satisfied(int i) = 0;  
    AndSpecification operator&& (Specification & other);
};

struct AndSpecification : Specification { ... }

inline Specification::operator&& (Specification & other) {
    return AndSpecification(*this, other);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM