1

I have been just tweaking around my game (putting class declaration in *.h files with their definition in the corresponding .cpp object files), but I don't quite understand when I have to use 'inline' keyword next to the method definitions and when I don't. Let me show you:

//shape.h
//header guards and includes in here
class shape
    {     
          private:
                  char type;
                  int verticies[4][2];
                  int centre[2];
                  int radius;
          public:
                 shape(int coordinates[4][2]);
                 shape(int coords[2], int r);
                 void Change(int coordinates[4][2]);
                 void Change(int coords[2], int r);
                 //more functions...
    };

//shape.cpp
#include "shape.h"

inline shape::shape(int coordinates[4][2])//Constructor for a rectangle shape
{                
            for (int i=0; i<8; i++) //copy arguments to class
             verticies[i/2][i%2]=coordinates[i/2][i%2];
             //calculate centre of the rectangle
             centre[0]=(coordinates[0][0]+coordinates[2][0])/2;
             centre[1]=(coordinates[0][1]+coordinates[2][1])/2;
}

inline shape::shape(int coords[2], int r)//Constructor for a circle shape
{
            type='C';
            centre[0]=coords[0];
            centre[1]=coords[1];
            radius=r;
}

inline void shape::Change(int coordinates[4][2])//change coordinates of a rectangle shape
{
            if (type=='C') return;//do nothing if a shape was a circle
             for (int i=0; i<8; i++)
             verticies[i/2][i%2]=coordinates[i/2][i%2];
             centre[0]=(coordinates[0][0]+coordinates[2][0])/2;
             centre[1]=(coordinates[0][1]+coordinates[2][1])/2;
             if(verticies[0][0]-verticies[1][0]==0 || verticies[0][1]-verticies[1][1]==0) type='S'; else type='R';
}

inline void shape::Change(int coords[2], int r)//change coordinates for a circle shape
{
        if (type=='R' || type=='S') return; //do nothing if a shape was not a circle
        centre[0]=coords[0];
        centre[1]=coords[1];            
        radius=r;
}
//and so on...

Not having an inline keyword would result in: "multiple definition of `shape::shape(int (*) [2])' " error. However on other occasions with other classes use of 'inline' was unneccesary. So my question is: when do I have to use 'inline' keyword and why is it important anyway?

Edit: So I have been informed that using inline in this situation is a bad Idea. Therefore what is a proper way to implement source and header files?

4

2 回答 2

3

inline当您在头文件中但在类声明之外定义非模板函数时,该关键字很重要。当标题包含在多个位置时,它避免了多重定义问题。

除此之外,这几天用处不大。从技术上讲,它仍然应该表明您希望一个函数使用内联扩展——即,编译器不实际调用它(这会产生很小的开销),而是将整个函数体的副本放到它被调用的任何位置。(对于非常小的函数,例如访问器方法,这是一个非常宝贵的优化。)但实际上,编译器倾向于自己找出应该内联和不应该内联的内容,因此它只接受关键字作为提示。

于 2013-11-08T20:24:47.877 回答
0

在您发布的代码版本中,inlinekey 完全没有必要并且实际上是有害的。在这个版本中,您必须从您发布的代码中删除所有inline关键字才能正确编译和链接它。由于缺少函数定义,您发布的内容通常会导致链接器错误。

inline如果将所有函数定义移动到头文件中,则关键字变得必要。

inline因此,这是关键字和成员函数必须遵循的简单规则:如果您在头.h文件 ( ) 中定义成员函数,请使用inline关键字。如果您在 implementation ( .cpp) 文件中定义您的成员函数,请不要使用inline关键字。

于 2013-11-08T20:35:40.953 回答