简体   繁体   English

C ++循环依赖

[英]C++ circular dependency

I have an object class, and an exception class derives from it. 我有一个对象类,一个异常类派生自它。 Now I want a method of object class throw an exception object but run into endless "base class not defined" and "incomplete type" problems. 现在我想要一个对象类的方法抛出异常对象,但遇到无休止的“基类未定义”和“不完整类型”问题。 Is this even possible? 这甚至可能吗?

Thanks. 谢谢。

Code: 码:

class object
{

public:
    virtual ~object(){ }

    virtual bool equals(){ throw new exception; }
    virtual int getHash(){ throw new exception; }
    virtual void getType(){ throw new exception; }
};

class exception :
    public object
{
private:
public:
    exception();
    virtual ~exception();
    virtual const char* info();
};

Don't define the member function in the class. 不要在类中定义成员函数。 Define it after the two classes have been defined. 在定义了两个类之后定义它。

在这种特定情况下,只需将类object的实现放在单独的cpp文件中,只在头中留下声明。

Do you need the exception class to inherit from the object class? 您是否需要从对象类继承异常类? You can define the exception class before the object class in your header file if you don't want separate cpp files. 如果不需要单独的cpp文件,可以在头文件中的对象类之前定义异常类。

    class exception {  
        private:
        public:
            exception();
            // rest of your code
    };

    class object {
        public:
            virtual ~object() {}
            virtual bool equals() { throw exception(); }
            // rest of your code
    };

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

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