Search code examples
c++clionmember-functions

Can CLion move an in-the-class method definition out-of-class?


Common background goes first. In C++, you can write a method definition right inside the class body, as illustrated in the following Effective-C++ styled Widget class:

class Widget {
  unsigned TheSize;
public:
  unsigned getSize() const { return TheSize; }
};

I know this fashion is also called in-the-class definition and it is inline implicitly (without telling the compiler with keyword). On the other hand, you can write the method out of the class, explicitly qualifying its name, like:

class Widget {
  unsigned TheSize;
public:
  unsigned getSize() const;
};

unsigned Widget::getSize() const {
  return TheSize;
}

Now comes the question. I want to transform some classes written in the in-the-class fashion to the out-of-class fashion, using CLion, the new IDE made by JetBrains for C/C++. I wonder whether this nice-looking IDE provides builtin support for this refactoring. I can't see it has a refactoring called Pull Inline Method Out of Class though it does have the opposite Inline Method refactoring tool.

Edit: The CLion version I am using:

CLion 2018.3.1
Build #CL-183.4588.63, built on December 4, 2018
Licensed to CLion Evaluator
Expiration date: January 10, 2019
JRE: 1.8.0_152-release-1343-b16 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Linux 4.15.0-42-generic

Solution

  • It is as @MaximBanaev said in the comments. I found no way to move all definitions into a .cpp file. So, what I did was, I created a New -> C/C++ Source File (with the same name as the .hpp file, but it's not mandatory) with Add to target ticked. Then I included the header file in the source file. After that, when I put the cursor on the method name, hit Alt + Enter, and selected Split function into declaration and definition, it left only the declaration in the header file, and automatically moved the definition into the source file.

    (Oh and, this doesn't work for templated classes.)