Search code examples
c++pointer-to-member

c++ , issues with return type of member functions in subclasses


Before I ask my question, I'll describe the scenario.

Let's say I have a class called BinaryOperator with 2 subclasses: And, Or.

I am wondering if it is possible for me to define a function f() in And with return type Or* like so:

Or* And::f()

And also define a function g() in Or with return type And* like so:

And* Or::g()

When I try to do this, I end up with a compile error saying along the lines of " 'Or' does not name a type".

Please, if you can, any clarity will be appreciated.


Solution

  • What you are looking for is something like this:

    class Or; // <-- forward declaration
    
    class And : public BinaryOperator
    {
    public:
        Or* f();
    };
    
    class Or : public BinaryOperator
    {
    public:
        And* g();
    };