Reputation: 18264
I am seeing the following pattern in a lot of some legacy code. I am not familiar with it and cannot fathom why it is there.
In the header file:
struct ook
{
bool func_called; // Not declared as const!
bool func();
ook();
};
In the source file:
ook::ook():
func_called( func() )
{} // ← Nothing there whatsoever.
bool ook::func()
{
// … ← Some stuff without a "return" statement of any kind.
return true; // This does mean that func_called is always true.
}
Is this some horrible copy-and-paste error from something useful or just oddness?
Now, func_called
is only called in the constructor and never else in the code. If it was, as kindly pointed out by Florian Castellane in his answer, it would make sense to have it. If it happened only once, I could imagine this used to be used. However, it happens several dozen times in the code base so I wonder what other use it could have.
Just to be extra clear, this is not my code. I am just trying to understand (and without history, comments, or unit tests) what the logic is writing it was. Maybe it's hopeless?
Upvotes: 0
Views: 133
Reputation: 477358
If what you're saying is true and nobody uses ook::func_called
, then your code is equivalent to the following, simpler code:
struct ook
{
ook() { func(); }
void func() { /* Some stuff */ }
};
You should pay great attention to whether copy construction and copy assignment behave as intended.
Upvotes: 1
Reputation: 1225
Assuming the rest of the code tests for true == func_called
, this could be used to ensure the structure is initialized using the constructor.
Upvotes: 4