C++ - 2 classes 1 file

后端 未结 8 2019
南旧
南旧 2021-01-19 03:11

Suppose I want something of this sort, in one .cpp source file:

class A {
    public:
        void doSomething(B *b) {};
};

class B {
    publi         


        
相关标签:
8条回答
  • 2021-01-19 03:39

    forward declare one class before other with

    class B;
    or
    class A;
    

    But still you won't be able to implement

    void doSomething(B *b)
    

    using only forward declaration of B. So you have to put definition of doSomething below full class A declaration

    0 讨论(0)
  • 2021-01-19 03:39

    Add another declaration of B before A:

    class B;
    
    class A {
        public:
            void doSomething(B *b) {};
    };
    
    class B {
        public:
            void doSomething(A *a) {};
    };
    
    0 讨论(0)
提交回复
热议问题