Declare an object even before that class is created

前端 未结 5 629
没有蜡笔的小新
没有蜡笔的小新 2021-02-04 14:03

Is there anyway to declare an object of a class before the class is created in C++? I ask because I am trying to use two classes, the first needs to have an instance of the sec

5条回答
  •  野的像风
    2021-02-04 14:34

    You can't declare an instance of an undefined class but you can declare a pointer to one:

    class A;  // Declare that we have a class A without defining it yet.
    
    class B
    {
    public:
        A *itemA;
    };
    
    class A
    {
    public:
        B *itemB;
    };
    

提交回复
热议问题