+ 4
Forward declaration only tells compiler about the existence of the class, and untill the definition appears, that class name has an incomplete type. This information can be used if you want to refer to a class before the actual definition itself ( via pointers or references as they don't need information about the size and members of the class ) In you perticular use case, you are not only trying to create an object of type "Nani", but also trying to access a data member (Nani::a) of it before the actual definition of class "Nani", needless to say it would throw an error as compiler doesn't have information about the same by then. --- You can read more about forward declaration here 👇 https://en.cppreference.com/w/cpp/language/class
21st Jun 2022, 2:28 AM
Arsenic
Arsenic - avatar
+ 1
I didn't see any class prototype exist till now... #include <iostream> using namespace std; class Nani ; //edit: this is valid, way Manav Roy class Nani { public: int a=5; }; class Baka { Nani n; public: void get() { cout<<n.a; } }; int main() { Baka b; b.get(); return 0; }
20th Jun 2022, 5:42 PM
Jayakrishna 🇼🇳