inline function members inside a class

前端 未结 3 1781
悲&欢浪女
悲&欢浪女 2020-12-23 09:24

I know that declaring a function (normal function not a method inside a class) as inline is a good practice when the function definition is small for performance and it save

3条回答
  •  礼貌的吻别
    2020-12-23 09:36

    There are two options to offer to the compiler to make a class function inline:

    (1) Defining a function in the declaration of the class (in a header file)

    class Human {
    
    public:
    
        Human(const char* name);
        Human();
    
        // is implicit inline
        void lookAt(const char* name) const {
            std::cout << "I'm looking at " << name << std::endl;
    
        }
    
    private:
        char _name[30]; 
    
    }; 
    

    (2) Using the inline keyword explicitly in the definition of the function (in a header file)

        // is explicit inline 
        inline void lookAt(const char* name) const {
            std::cout << "I'm looking at " << name << std::endl;
    
        }
    

提交回复
热议问题