Search code examples
c++header-filescode-structure

C++: Avoid .cpp files with only an empty (de)constructor


When I have a header file like this:

#ifndef GAMEVIEW_H_
#define GAMEVIEW_H_

#include <SDL/SDL.h>

class GameView
{
public:
 GameView();
 virtual ~GameView();

 virtual void Update() = 0;
 virtual void Render(SDL_Surface* buffer) = 0;
};

#endif /* GAMEVIEW_H_ */

I need to create a .cpp file like this:

#include "GameView.h"

GameView::~GameView()
{

}

GameView::GameView()
{
}

This is a bit stupid. Just a .cpp file for an empty constructor and deconstructor. I want to implement that method simply in the header file. That is much cleaner.

How to do this?


Solution

  • You can define your constructor and destructor (this is the proper term, use this instead of deconstructor) inline:

    class GameView
    {
    public:
     GameView() {}
     virtual ~GameView() {}
    
     virtual void Update() = 0;
     virtual void Render(SDL_Surface* buffer) = 0;
    };