Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
155 views
in Technique[技术] by (71.8m points)

c++ - Enums: Can they do in .h or must stay in .cpp?

If I have something like:

enum
{
    kCP_AboutBox_IconViewID = 1,
    kCP_AboutBox_AppNameViewID = 2,
    kCP_AboutBox_VersionViewID = 3,
    kCP_AboutBox_DescriptionViewID = 4,
    kCP_AboutBox_CopyrightViewID = 5
};

in my .cpp can it go in the .h?

More so, what other lesser know things can you put in a .h besides class definitions, variables, etc, etc

question from:https://stackoverflow.com/questions/1284529/enums-can-they-do-in-h-or-must-stay-in-cpp

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A .h file is essentially just code which, at compile time, is placed above any .cpp (or .h file for that matter) that it's included in. Therefore you CAN just place any code from the .cpp file into the .h and it should compile fine.

However it's the design which is important. Your code (e.g. your enum) SHOULD be placed in the .h file if you need to expose it to the code you're including the .h file. However if the enum is only specific to the code in your header's .cpp implementation, then you should encapsulate it just within the .cpp file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...