anon
anon

Reputation: 42677

C++ "class Foo return Bar" and "class Bar return Foo" possible?

Is it possible to define C++ classes Foo and Bar s.t.

class Foo {
  Bar makeBar();
};

class Bar {
  Foo makeFoo();
};

?

Thanks!

Upvotes: 1

Views: 440

Answers (2)

Ray Hidayat
Ray Hidayat

Reputation: 16229

Yes it is, you just have to put forward declarations at the top.

class Foo;
class Bar;

class Foo {
  Bar makeBar();
};

class Bar {
  Foo makeFoo();
};

Upvotes: 8

Reed Copsey
Reed Copsey

Reputation: 564691

Yes. You can do it with a forward declaration.

For example, in Foo.h, add:

class Bar;

class Foo {
    Bar makeBar();
};

Upvotes: 2

Related Questions