I was wondering if it is bad to have an include in the main loop. I have several blocks of code that are a couple hundred lines each. I only need to call them once, so using a function would be unneeded IMO.
Here is an example of what I am talking about.
foo.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
int main() {
while (true) {
#include "bar.cpp"
}
return 0;
}
bar.cpp
std::cout << "foobar\n";
This code works, but I am not sure if it is good technique..
It's not so much poor technique as a clumsy way of implementing pseudo-functions. Why not just have functions? The extra overhead really isn't going to kill you, especially if you only call them once. This is what functions are for.
You could use inlining if you want to give the compiler the option of this.
Yeah, I was thinking about using functions.. but I was reading an article that was basically saying if you only need to use a code block once, then it is better to just have the code directly in the place that it should be.
Seeing as I have never seen someone use an include in this way, I won't. But I will leave this thread open for a little longer just to see others' opinions.
but I was reading an article that was basically saying if you only need to use a code block once, then it is better to just have the code directly in the place that it should be.
Please post a link and let me know who publishes such nonsense.... o_O
its not a back technique but its best if you do grouping in your program becos it gives clarity and understanding if any programmer caries the codes to read.
@Galik what I was wondering is if that is bad technique or not, for every other program I've created I just used functions.. but some people say it is bad technique to use a function if it is only going to be used once. But my question was answered by other posters.