Search code examples
c++syntaxinline

How to let inline functions from different classes call each other


To make some core-codes run as fast as possible, I want to make three functions inline, those belong to two classes respectively.

Ideally, I want to define them as following:

class ClassB;
class ClassA {
    ClassB * pB;

    inline void func1() {
        // do something
    };

    inline void func2() {
        pB->func3();
    };
};

class ClassB {
    ClassA * pA;

    inline void func3() {
        pA->func1();
    };
};

I know it's impossible if I simplly code as above. My question is : Is there a way in it, we can do similar things? At least, let them run as fast as possible. Thanks!


Solution

  • You can define the functions outside the class definitions and still inline them.

    class ClassB;
    class ClassA {
       ClassB * pB;
    
       void func1();
    
       void func2();
    };
    
    class ClassB {
       ClassA * pA;
    
       void func3();
    };
    
    inline void A::func1()
    {
       // do something
    }
    
    inline void A::func2()
    {
       pB->func3();
    }
    
    inline void B::func3()
    {
       pA->func1();
    }
    

    I am not sure how the compiler will generate code for mutually referencing inline functions but they are fine from a synactic point of view.