Visual Studio : hotkey/way to automatically sort functions in .h to be consistent with .cpp

和自甴很熟 提交于 2019-12-05 07:42:30

As the other answer has pointed out, there does not seem to be any automatic way of doing what you would like to do.

However, you can make the process easier on you by following coding standards.

First of all, you ask:

Primary Question: Is there any (semi) automatic way to do (.cpp -> .h) in Visual Studio? Hotkey? Plugin?

I don't think that is the right approach. You don't want the layout of your .cpp file to drive the layout of your .h file. The .h file is the interface. Make the layout of the .h file so that it makes sense to you and your users. Then make sure that .cpp file is laid out in way that makes sense in relation to the .h file.

Here's 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 a 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 such layout, it will be easier to make sure that order in which function declarations appear in the .h file are followed in the .cpp file.

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

This is just my opinion!

There can't be such functionality because of following reasons:

  • Sorting *.h file cause mixing public, protected and private methods, better is to have these section separated then you see immediately in which group method belongs to.

  • You should tell which *.h file you want to sort, because in one *.cpp you can have definitions of multiple classes which are declared in multiple *.h files.

  • You should tell which *.h file you want to sort, because *.cpp file can have different name trom *.h

There's no button or shortcut for this. You just have to manually move it using cut-move or the normal way which is the select the code and drag it to your desired position.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!