Reputation: 1966
My team and I are starting a game project in C++. According to our professor, header files should only include interfaces, and source (.cpp) files should contain the implementation of our code found in our header file. However, after fooling around in Visual Studio, I noticed that the auto-generated code, say for a button click, is thrown in the header file with a body ready to implemented.
Game.h
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
}
Where I would have expected it to be:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e);
And then implement it in our source file.
Game.cpp
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
// Code here
}
Is there a reason why this is done like this? Must I really modify the code in order to follow proper standards of separation between interface and implementation?
Upvotes: 0
Views: 1047
Reputation: 11
Technically, the auto generation is Visual Studio did is correct in terms of being able to compile. However, what your professor instructed is more conventional.
Upvotes: 1
Reputation: 941515
It is a simple way the limit the amount of hassle you have to deal with when you modify the UI design. Adding a new event handler is pretty straight-forward, the trouble starts when it needs to be removed again. Say when you remove the button from the window.
The way it is done now, code is generated in only two places, the InitializeComponent() method and the Click event handler method itself. Removing the event is simple, the designer just removes the statement in InitializeComponent() and you will not lose any code you've written in the Click event handler. And it is simple to rescue or abandon any code you've written, you just copy/paste and delete the method. Either way, the project still compiles.
Lots more hassle with code generated in three places. Neither deleting the declaration in the .h file and having the project not compile anymore nor deleting any code you've written in the method are attractive choices.
Absolutely nothing to fret about, simply move the method body into the .cpp file if that's you're preference. You do this when the design is stable. Or not at all because it works just fine as-is.
Upvotes: 2