I’ve been recently working on a C++ project and ran into a task where I had to move many inline header functions out of the header and into the corresponding .cpp file.
Visually, I wanted to convert:
Biscuit.h
class Biscuit
{
public:
virtual void Taste(int chompiness) { m_ChompRating = chompiness; }
private:
int m_ChompRating;
}
into:
Biscuit.h
class Biscuit
{
public:
virtual void Taste(int chompiness);
private:
int m_ChompRating;
}
Biscuit.cpp
void Biscuit::Taste(int chompiness)
{
m_ChompRating = tastiness;
}
We’ve all been there. It is usually an exercise in our copy and paste skills, and an opportunity to work on our RSI. I thought there must be a better way! So I scoured the Internet but no one seems to talk about it. It seems every programmer has just accepted that the only way to get this done is to stop mucking about on Google and push your fingers to the keys.
I was still not satisfied. I asked the collective brains of Stack Overflow and the only solution I got was to purchase Visual Assist. I’ve tried Visual Assist. It is a brilliant product. It is also a brilliantly expensive product. Nawaz, from Stack Overflow (he is the one that looks like a baby) challenged me to write a macro myself, and share it, because “it wouldn’t be too difficult to write”.
Hah! It was not that difficult to get something that worked some of the time. It was difficult to create something that worked all of the time. I knew nothing about macros, other than how to record them and see the generated code. Finally, after a bit of reverse engineering, a tiny bit of reading and some code stealing inspired coding, I have a set of macros I am quite happy with. Over my next 3 blog posts, I will share these macros with you, and on the way, we will build up a few good utilities:
- How to flip between the header and cpp file
- Automatically adding an implementation skeleton to the cpp file from the header definition
- Moving a function defined inline in the header into the cpp file
And hopefully, some Googler will be able to make use of these macros. See you soon.