{{< Draft >}}
The method has a parameter that refers to the object that the method will become an instance of. Example:
// Const parameter
void F(const Cls& cls)
{
}
// will eventually become:
class Cls
{
void F() const; // <-- notice the const on the end
}
// Non-const parameter
void F(Cls& cls)
{
}
// will eventually become:
class Cls
{
void F(); // <-- notice there is NO const on the end
}
Apply the Convert Free Function to Static Method (fix link) refactoring to:
- move the function to the compilation unit (.cpp file) containing the class implementation.
- make the function a static method of the class.
class Cls
{
public:
static void F(const Cls& cls);
}
becomes
class Cls
{
public:
static void F(const Cls& cls) { cls.F(cls); }
void F(const Cls& cls);
}
Do this with copy and paste, not by re-typing.
b. If the class instance parameter is const, put a const at the end of the instance method's signature in BOTH the header and implementation files:
// Header file
class Cls
{
public:
static void F(const Cls& cls) { cls.F(cls); }
void F(const Cls& cls) const; // <-- add const at the end
}
// Implementation file
void Cls::F(const Cls& cls) const // <-- add const at the end
{
cls.blah();
}
Note: you can find callers by commenting out the static method, compiling, and looking for compiler errors.
Do a few instances, then uncomment the static method, compile, and commit. Repeat until done (or bored).
- Find the argument that corresponds to the class parameter.
- If it is not a simple variable name, then perform an Extract Variable refactoring to create a variable name.
- Copy the variable name.
- Highlight the class's name plus the scope resolution operator that is in front of the function's name.
- Paste the variable name in front of the function's name, followed by a dot, to create an instance method call.
If you updated all callers, then remove the static inline function.
void Cls::F(const Cls& cls)
{
(*this).blah(); // <-- replace 'cls' with (*this)
}
- In the class's header file, remove the class parameter from the function's signature.
- Do the same in the function's implementation.
- Compile. For each error:
- Remove the class parameter.
- Compile & Commit