Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

Example

B.h

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

B.cpp

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

Expected Result (B.h)

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

Question

Primary 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 the opposite way (it has no solution).
Secondary question: How to do the opposite way (semi) automatically? (.h -> .cpp)

It is a basic feature. It should exist, right?
Otherwise, how would experts do about the disorder? Do they move it manually?

Edit:
All of the three answer states that there are no such automatic ways, but I still hope there are.

like image 551
javaLover Avatar asked Jan 11 '17 03:01

javaLover


3 Answers

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.

like image 119
R Sahu Avatar answered Nov 11 '22 16:11

R Sahu


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

like image 4
Jirka Picek Avatar answered Nov 11 '22 17:11

Jirka Picek


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.

like image 3
CraftedGaming Avatar answered Nov 11 '22 17:11

CraftedGaming