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?