Visual Studio: hotkey / way to automatically sort functions in .h format to match .cpp

I want to make the order of the functions in the .h header consistent with the order of the functions in the .cpp source file.

Example

Bh

 class B{ void f2(); //<--- wrong order void f1(); }; 

B.cpp

 #include "Bh" void B::f1(){} void B::f2(){} 

Expected Result (Bh)

 class B{ void f1(); //<---- nice void f2(); }; 

Question

Main question: Is there any (semi) automatic way to do ( .cpp -> .h ) in Visual Studio?
Hotkey? Script? Plugin? Visual AssistX / Resharper?

There is a similar question , but it asks for the return path (it has no solution).
Secondary question: How to make the return path (semi) automatically? ( .h -> .cpp )

This is a basic feature. It must exist, right?
Otherwise, how will the experts do the discord? Do they move it manually?

Edit:
All three answers say that there are no such automatic paths, but I still hope they are.

+6
source share
3 answers

As another answer pointed out, there is no automatic way to do what you would like to do.

However, you can simplify the process by following coding standards.

First of all, you ask:

Main question: Is there any (semi) automatic way to do (.cpp β†’ .h) in Visual Studio? Hotkey? Plugin?

I do not think this is the right approach. You do not want the layout of your .cpp file to control the layout of your .h file. An .h file is an interface. Layout the .h file so that it makes sense for you and your users. Then make sure that the .cpp file is laid out in a way that makes sense with the .h file.

Here is one layout for .h files that makes sense to me.

 //--------------------------------------------------------------------- /*! \file MyClass.h Copyright Notice ... \author Your Name \date 2017-Mar-01 */ //--------------------------------------------------------------------- #pragma once #ifndef MyClass_H #define MyClass_H // ********** BEGIN STANDARD INCLUDES ********** // ********** END STANDARD INCLUDES ********** // ********** BEGIN EXTERN DECLARATIONS ********** // ********** END EXTERN DECLARATIONS ********** // ********** BEGIN FORWARD DECLARATIONS ********** // ********** END FORWARD DECLARATIONS ********** // ********** BEGIN TYPEDEF DEFINITIONS ********** // ********** END TYPEDEF DEFINITIONS ********** // ********** BEGIN MACRO DEFINITIONS ********** // ********** END MACRO DEFINITIONS ********** // ********** BEGIN ENUM DEFINITIONS ********** // ********** END ENUM DEFINITIONS ********** /*! \class MyClass This class does this and that. */ class MyClass { public: protected: private: }; // ********** BEGIN INLINE FUNCTIONS ********** // ********** END INLINE FUNCTIONS ********** // ********** BEGIN EXTERN FUNCTIONS ********** // ********** END EXTERN FUNCTIONS ********** #endif 

And the layout for the corresponding .cpp file:

 //--------------------------------------------------------------------- /*! \file MyClass.cpp Copyright Notice ... \author Your Name \date 2017-Mar-01 */ //--------------------------------------------------------------------- #include "MyClass.h" // ********** BEGIN STANDARD INCLUDES ********** // ********** END STANDARD INCLUDES ********** // ********** BEGIN EXTERN DECLARATIONS ********** // ********** END EXTERN DECLARATIONS ********** // ********** BEGIN STATIC DECLARATIONS ********** // ********** END STATIC DECLARATIONS ********** // ********** BEGIN EXTERN DEFINITIONS ********** // ********** END EXTERN DEFINITIONS ********** // ********** BEGIN HELPER CLASSES ********** // Namespace for helper classes and functions used in the file. namespace MyClassNS { } using namespace MyClassNS; // ********** END HELPER CLASSES ********** // ********** BEGIN PUBLIC FUNCTIONS ********** // ********** END PUBLIC FUNCTIONS ********** // ********** BEGIN PROTECTED FUNCTIONS ********** // ********** END PROTECTED FUNCTIONS ********** // ********** BEGIN PRIVATE FUNCTIONS ********** // ********** END PRIVATE FUNCTIONS ********** 

With this layout, it will be easier to make sure that the order in which function declarations appear in the .h file is in the .cpp file.

PS You don’t need to use Doxygen style markups in your code unless you use it to automate the creation of documentation.

+4
source

This is just my opinion!

There can be no such functionality for the following reasons:

  • Sorting a * .h file causes mixing of public, protected and private methods, it is better that this section be divided, and you will immediately see in which group method it belongs.

  • You must specify which * .h file you want to sort, because in one * .cpp you can have definitions of several classes that are declared in several * .h files.

  • You must specify which * .h file you want to sort, because the * .cpp file may have a different name trom * .h

+4
source

There is no button or shortcut for this. You just need to manually move it using cut or the usual method that selects the code and drags it to the desired position.

+3
source

Source: https://habr.com/ru/post/1013966/


All Articles