What's new

C & C++ need opinion regards to header files

Status
Not open for further replies.

fireclouu

Honorary Poster
Joined
Jun 1, 2014
Posts
440
Solutions
1
Reaction
152
Points
190
hello! diretsuhin ko na, sa c/c++ paano niyo hinahandle variable/function declarations sa header? nagconduct na ko ng research ko bago magtanong dito, at karamihan sa kanila "dont define variables in header", na contradict mismo sa ginagawa ko kasi lagi kong dinedefine vars. ko sa header, madalas kasi kailangan ko bilang "global variable" sa lahat ng .c file na kailangan siya:
e.g
C:
// file.h
...
int a;
...

C:
// main.c
...
int main()
{
   a = 5;
}
...

C:
// sub.c
...
void func()
{
   ...
   a = someOp;
}
...

rason kung bakit ito 'yung practice ko, opinion ko lang, eh mas madali ko kasing mahanap yung pinagmulan ng source kung saan ko dinefine kaysa i-extern ko, hindi na ko titingin sa main.c, sub.c, etc, at kung sobrang daming var. , eh iisa na lang yung bubuksan ko. Aware ako sa "multiple definition error" kaya depensa ko i-wrap sa "#ifndef" , ang cost lang (trivial) eh halos lahat yata ng header files ko meron nito. May mga kilala rin kasi akong ganito rin ang procedure, lalo na sa structs and union.
 
Last edited:
Because you don't want to pollute with global variables other files who wish to use your headers? In addition, those files might not have a need for those variables.
 
Because you don't want to pollute with global variables other files who wish to use your headers? In addition, those files might not have a need for those variables.
thanks, consider ko 'to :) pero diba single instance na naman vars. pag naka-ifndef na? or may side effect siya? I intend to do how java do it with "static" keyword.
 
Last edited:
close ko na, impractical pala way ko, standard pala talaga "extern" as global defs, either in .c or .h
 
Status
Not open for further replies.
Back
Top