Move Method (C++)
- 2 minutes to read
In This Article
Inlines a method body into the class declaration (in a header file), or inversely moves a method’s implementation to a source file leaving the declaration in the header file.
#Purpose
This refactoring makes it much easier to move method bodies between header and source files. This allows you to move all code to the same place without much formatting work.
#Availability
Available from the context menu or via shortcuts:
- when the edit cursor or caret is on a method declaration. If the declaration and implementation are both within a header file, you can move the implementation to the corresponding source file. If you are on a method declaration within a source file, you can move the implementation to the corresponding declaration within the header file.
#“Move Method to Header” example
// The .h file
#pragma once
using namespace System;
ref class TestClass
{
private:
int m_ID;
String^ m_Text;
public:
TestClass(void);
void SetValues(int id, String ^text);
};
// The .cpp file
#include "StdAfx.h"
void TestClass::│SetValues(int id, String ^text)
{
m_ID = id;
m_Text = text;
}
Result:
// The .h file
#pragma once
using namespace System;
ref class TestClass
{
private:
int m_ID;
String^ m_Text;
public:
TestClass(void);
void SetValues(int id, String ^text)
{
m_ID = id;
m_Text = text;
}
};
// The .cpp file
#include "StdAfx.h"
#Screenshot
#“Move Method to Source File” example
// The .h file
#pragma once
using namespace System;
ref class TestClass
{
private:
int m_ID;
String^ m_Text;
public:
TestClass(void);
void │SetValues(int id, String ^text)
{
m_ID = id;
m_Text = text;
}
};
// The .cpp file
#include "StdAfx.h"
Result:
// The .h file
#pragma once
using namespace System;
ref class TestClass
{
private:
int m_ID;
String^ m_Text;
public:
TestClass(void);
void SetValues(int id, String ^text);
};
// The .cpp file
#include "StdAfx.h"
void TestClass::SetValues(int id, String ^text)
{
m_ID = id;
m_Text = text;
}