views:

137

answers:

2

I know that I can forward declare a class as follows:

class Foo;

// ... now I can use Foo*

However, can I do something like this:

class Bar {
  public:
    virtual void someFunc();
};
// ... somehow forward declare Class Foo as : public Bar here
someFunc(Foo* foo) {
  foo -> someFunc();
}

class Foo: public Bar {
}

?

Thanks!

+4  A: 

You can forward declare Bar as class Bar; and change the signature of someFunc to take a Bar* as parameter. Since someFunc() is a virtual method in the base class, it should work.

Naveen
+1  A: 

After your forward declaration Foo becomes an incomplete type and it will remain incomplete until you provide the definition of Foo. While the Foo is incomplete any attempt to dereference a pointer to Foo is ill-formed. So to write

foo->someFunc();

you need to provide Foo's definition.

Tadeusz Kopec