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?

有帮助吗?

解决方案

The inline keyword is important when you define a non-template function in a header file, but outside a class declaration. It avoids the multiple definition issue when a header is included in multiple places.

Other than that, it's not much use these days. It's technically still supposed to indicate that you want a function to use inline expansion -- i.e. instead of actually calling it (which presents a small overhead), the compiler just drops copies of the entire function body into whatever location it gets called from. (It's an invaluable optimisation for very small functions, such as accessor methods.) In reality though, compilers tend to figure out what should and shouldn't be inlined on their own, so it takes the keyword as little more than a hint.

其他提示

In the version of the code you posted, inline key is completely unnecessary and actually harmful. In this version you have to remove all inline keywords from the code you posted to be able to properly compile and link it. What you posted will typically cause linker errors due to missing function definitions.

inline keyword becomes necessary if you move all your function definitions into the header file.

So, this is the simple rule you have to follow with inline keyword and member functions: If you define your member function in the header (.h) file, use inline keyword. If you define your member function in implementation (.cpp) file, don't use inline keyword.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top