简体   繁体   English

C++:如何转发声明出现在基类 class 的 static 方法中的派生类?

[英]C++: How can I forward-declare derived classes that appear in a static method of a base class?

Just doing a simple exercise where I'm translating ideas I learned from another language to C++.只是做一个简单的练习,将我从另一种语言学到的想法翻译成 C++。

I have an abstract class Number, which has two derived classes, PositiveNumber and NegativeNumber.我有一个抽象的 class Number,它有两个派生类,PositiveNumber 和 NegativeNumber。 Number has a static method that should create a new instance of either PositiveNumber or Negative number, depending on the sign of its input. Number 有一个 static 方法,它应该根据输入的符号创建 PositiveNumber 或 Negative number 的新实例。

#include <iostream>

class Number
{
public:
protected:
    int magnitude_;
public:
    static Number* fromInt(int x)
    {
        if (x >= 0) { return new PositiveNumber(x); }
        else        { return new NegativeNumber(x); }
    }
    int getMagnitude() { return magnitude_; }
    virtual void print() = 0;
};

class PositiveNumber: public Number
{
protected:
public:
    PositiveNumber(int magnitude) { magnitude_ = magnitude; }
    void print() { std::cout << magnitude_ << "\n"; }
};

class NegativeNumber: public Number
{
protected:
public:
    NegativeNumber(int magnitude) { magnitude_ = magnitude; }
    void print() { std::cout << "-" << magnitude_ << "\n"; }
};


int main (int argc, char* argv[])
{
    Number* x = Number::fromInt(5);
    x->print();

    return 0;
}

I know that I need to tell Number that PositiveNumber and NegativeNumber exist, but I'm not sure how to do that.我知道我需要告诉 Number PositiveNumber 和 NegativeNumber 存在,但我不确定该怎么做。 I tried adding我尝试添加

class PositiveNumber;
class NegativeNumber;

before Number's definition, but that wasn't enough, and resulted in:在 Number 的定义之前,但这还不够,结果是:

use of undefined type 'PositiveNumber'
use of undefined type 'NegativeNumber'

I'm sure this has a simple answer, but I'm pretty new to debugging C++ stuff, so I'm pretty lost.我确定这有一个简单的答案,但我对调试 C++ 的东西还很陌生,所以我很迷茫。 Thanks for reading.谢谢阅读。

The definition of fromInt() needs to know what constructors PositiveNumber and NegativeNumber have, so forward declarations are not enough. fromInt()的定义需要知道PositiveNumberNegativeNumber有什么构造函数,所以前向声明是不够的。 You need to break up the declaration and definition of Number::fromInt() , and then you can move the definition underneath the declarations of PositiveNumber and NegativeNumber .您需要分解Number::fromInt()的声明和定义,然后您可以将定义移动到PositiveNumberNegativeNumber的声明下方。

Also, don't forget to delete the object that fromInt() new 's.另外,不要忘记delete fromInt() new的 object。 Which also means adding a virtual destructor to Number so derived destructors can be called correctly from a base Number* pointer.这也意味着向Number添加一个虚拟析构函数,以便可以从基数Number*指针正确调用派生的析构函数。

Try this:尝试这个:

#include <iostream>

class Number
{
protected:
    int magnitude_;
public:
    static Number* fromInt(int x);

    virtual ~Number() {}

    int getMagnitude() { return magnitude_; }
    virtual void print() = 0;
};

class PositiveNumber: public Number
{
public:
    PositiveNumber(int magnitude) { magnitude_ = magnitude; }
    void print() { std::cout << magnitude_ << "\n"; }
};

class NegativeNumber: public Number
{
public:
    NegativeNumber(int magnitude) { magnitude_ = magnitude; }
    void print() { std::cout << "-" << magnitude_ << "\n"; }
};

Number* Number::fromInt(int x)
{
    if (x >= 0) { return new PositiveNumber(x); }
    else        { return new NegativeNumber(x); }
}

int main (int argc, char* argv[])
{
    Number* x = Number::fromInt(5);
    x->print();
    delete x;

    return 0;
}

Online Demo在线演示

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

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