Не холивара ради, сравнение кода Swift и C++:
class Object {
let inventory = Inventory()
}
class Inventory {
var items = [Object]()
}
class Human: Object {}
class Sandals: Object {}
class Socks: Object {}
let human = Human()
human.inventory.items.append(Socks())
human.inventory.items.append(Sandals())
print("Items count: \(human.inventory.items.count)")
Собираем, запускаем:
swiftc includeEachOther.swift -o includeTest && ./includeTest
Items count: 2
C++:
#include <vector>
#include <iostream>
using namespace std;
class Object {
public:
Inventory *inventory;
};
class Inventory {
public:
vector<Object *> items;
};
class Human: public Object {};
class Sandals: public Object {};
class Socks: public Object {};
int main() {
auto human = new Human();
human->inventory = new Inventory();
human->inventory->items.push_back(new Sandals());
human->inventory->items.push_back(new Socks());
cout << "Items count: " << human->inventory->items.size() << endl;
return 0;
};
Собираем, запускаем:
g++ -std=c++11 includeEachOther.cpp -o includeTest && ./includeTest
includeEachOther.cpp:10:2: error: unknown type name 'Inventory'
Inventory *inventory;
^
Если добавить forward declaration - class Inventory в .cpp, то все заработает. Я не понимаю почему в C++ нужно это в 2017 году, кто-нибудь может объяснить?