Reputation: 337
Ex.
I have a function that changes the pixel data (Red,Green,Blue) that has no parameters and uses a global struct, glob
. I would access glob.data
to read the Red,Green,Blue pixels.
What is the difference be if I were to include a parameter which would be a glob*
, I could access the data from the pointer.
Is this better practice then using the global from any function?
This implementation would be better than calling the 'glob.width = 2' etc
RevolverFilter(&global, &temp);
void RevolverFilter(glob* org,glob* temp1){
for x: for y: tRed = org->data[x+y*w].r;
...
}
Upvotes: 0
Views: 223
Reputation: 422
Encapsulating data and functionality into objects is a basis in Object Oriented Design. Using a global for passing values to a function is therefore not a "good" OOD practice, and generally not a good practice even for non-OOD designs, in structural languages such as C, as it is detrimental to the modularity of code components, and creates "hidden" dependencies in the code. If you absolutely need some kind of "global" like functionality, you can declare a static member in a class, making the member unique yet keeping it encapsulated into one component (a class).
Upvotes: 0
Reputation: 42083
"Is this better practice then using the global from any function?"
Using global variables as an alternative for passing a parameter to a function is definitely NOT a good practice.
If you want to create a function that will process (modify) pixel, then define a function that processes pixel
void processPixel(Pixel& p) {
...
}
Upvotes: 3