5

I'm working on a project where I'm converting C++ code to C# manually. I have working knowledge of C# but I've never used C++ before.

What I need to know is how to deal with the header files, since C# does't have anything like that. Say I have buffer.h and buffer.cpp, would I just convert them both and include them in the same buffer.cs file?

Is the C++ header file in any way related to an Ada spec file?

5
  • 1
    No you can't do that. Everything goes into *.cs files. Probably you know that already :). So the declaration and the definition is separated as a good practice in most of the cases in c++, but in .net the purpose of headers files is replaced by the concept of metadata. Short explanation. So yes you need to put everything in the same compilation unit .cs file. Commented May 4, 2012 at 13:50
  • The following link may be useful msdn.microsoft.com/en-us/magazine/cc301520.aspx Commented May 4, 2012 at 13:50
  • The header file is just textually included in the C++ translation unit at the point where the #include statement is written. So in effect, even in C++, there is just a single source file for the translation unit, once the inclusion has been made. Commented May 4, 2012 at 13:51
  • Probabily is very complicated because c++ have pointers and .h structure files, Struture of this two languages is very diferent. Commented May 4, 2012 at 14:02
  • Depending on how modern the C++ you're trying to translate is, a "literal" translation may be impossible. Of course you should be able to get good functional equivalency, but that will require a good understanding of both languages. Commented May 4, 2012 at 14:16

2 Answers 2

6

The distinction between includes ".h files" and source ".cpp files" is only one of convention. The convention is that declaration (functions, classes, etc) are in .h files which are #included in implementation (definition), or .cpp files. For most cases you're fine in collapsing X.h and X.cpp to a single X.cs file.

That said, you still need to take a look at what is going on in each file. A basic understanding of C++ would go a long way here, and something I strongly recommend you acquire before you get too far into your translation.

Sign up to request clarification or add additional context in comments.

Comments

1

It might help you to think of a C++ header file as containing two major types of things: the class definition, which defines its member data and "interface" (not to be confused with a C# interface type) and "other stuff". The class definition part contains method prototypes and class member variables.

The good news concerning the prototypes is that you simply don't need them in C#. Clients of your class receive prototype information from the implementation itself via the assembly for the namespace. The data members are contained within your C# class implementation, typically as private members which are exposed through C# properties.

The "other stuff" mentioned above can include #defines, which you typically want to turn into const definitions in C#. Other things such as enumerations have equivalents in C# which you of course move into the .cs file for your class.

2 Comments

#define macros in C and C++ can go far beyond constant variable declaration.
@luke - Good point. #define'd macros would have to be converted into methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.