Group: comp.lang.c++
From: "Bryan Parkoff"
Date: Friday, April 11, 2008 6:02 PM
Subject: Re: Pass by Reference Function Question

"Christopher" wrote in message
news:8b47ffd8-a4c8-4528-beab-46a7bd055cfd@8g2000hsu.googlegroups.com...
> On Apr 11, 5:00 pm, "Bryan Parkoff" wrote:
>> > On Apr 11, 3:24 pm, "Bryan Parkoff" wrote:
>> >> I write my large project in C++ source code. My C++ source code
>> >> contains approximate four thousand small functions. Most of them are
>> >> inline. I define variables and functions in the global scope.
>>
>> > OK
>>
>> I see you understand now.
>>
>> >> The global
>> >> variables and global functions are hidden to prevent from accessing by
>> >> the
>> >> programmers.
>>
>> > Then they aren't global are they?
>> > What means did you use to "hide" them?
>>
>> Well, you can make one of two choice -- global object or local
>> object.
>> The local object is ideal for class. You can always bind local variable
>> and
>> local function inside class. I have chosen to use global object. I
>> agree
>> that global variable should be avoided unless you put global object
>> inside
>> namespace for better readability. Global object has concern of
>> performance
>> issue than local object like class. Anyway...
>>
>> >> All global functions share global variables.
>>
>> > OK
>>
>> >> Only very few global functions are allowed to be reusability for
>> >> the
>> >> programmers to use.
>>
>> > What do you mean?
>> > You have global functions that you don't want anyone to call? They
>> > probably shouldn't be global then.
>>
>> You call public global function. Then public global function is in
>> turn
>> to call hidden global functions. I do not want the programmers to see
>> hidden global functions and they are allowed to use global object by
>> running
>> public global function.
>>
>>
>>
>> >> Few global functions access most hidden functions
>> >> during the time execution.
>>
>> > I thought the global functions were hidden themselves from what you
>> > said earlier...
>> > So, you have global "hidden" functions (I don't know how) calling
>> > other global "hidden" functions?
>>
>> >> My question is -- do you think that pass by reference is really
>> >> necessary?
>>
>> > Necessary for what?
>> > What are you trying to do? show some source code.
>>
>> >> Pass by reference is necessary unless you want to reuse function
>> >> with the choice of global / local variables.
>>
>> > What? I don't know what you are talking about, but no. There is no
>> > requirement to pass by reference based on global functions or
>> > variables or lack thereof.
>>
>> Let me give you sample code. It helps you to understand better. You
>> can see that pass by reference is not necessary because all hidden
>> functions
>> share global variable.
>>
>> // Global object
>> namespace Object
>> {
>> static int a = 0; // hidden global variable
>> static int b = 0; // hidden global variable
>> static int c = 0; // hidden global variable
>>
>> static void Modify1 (void); // hidden global function
>> static void Modify2 (void); // hidden global function
>> static void Modify3 (void); // hidden global function
>> void Run_Object (void); // public global function
>>
>> void Modify1 (void)
>> {
>> a += 2;
>> b += 4;
>> c = a * b;
>> }
>>
>> void Modify2 (void)
>> {
>> a *= 5;
>> b *= 10;
>> c = a - b;
>> }
>>
>> void Modify3 (void)
>> {
>> a = b / c;
>> b = 0;
>> c = 0;
>> }
>>
>> void Run_Object (void)
>> {
>> Modify1();
>> Modify2();
>> Modify3();
>> }
>>
>> }
>>
>> // end of header
>>
>> #include "object.h" // above code
>>
>> int main (void)
>> {
>> // do something...
>> Object::Run_Object(); // Run fine for all hidden functions.
>>
>> Object::Modify1 (); // Error Time Compile -- you can't access hidden
>> global functions.
>> a = 5; // Time Compile runs fine unless you use global variable -- a
>> outside main function on this source like main.cpp
>> Object::a = 10; // Error Time Compile -- you can't access hidden
>> global
>> variable
>> return 0;
>>
>> }
>>
>> You can see that namespace is ideal for readability because sometimes
>> global variable has the same name in both source codes. It guards
>> against
>> overwritten accidently.
>> Please advise.
>>
>> Yours Truly,
>> Bryan Parkoff
>
Christopher,

> Oh my goodness where to begin...

Thank you for your comment.

> Do not write implementation in header files. Write it in .cpp files
> Your source is obviously not what you ran, because the compiler gives
> no such error: "you can't access hidden global variable"

I agree. I always declare variables and functions in the header code
and I always define variable and functions in the source code. It would
help to debug a lot better. After you complete designing both header code
and source code, you will be able to create static library or DLL library.
Then, source code is not necessary. You can always use header code and it
will access to "LIB" or "DLL" file at compile time.

> Yes, passing anything at all, by reference or value, is not necessary
> in this example code, because the variables in question are global to
> the namespace in which the functions that use them, belong to. Passing
> them by reference is not necessary because they are also primitive
> types. Passing them by reference is also not necessary because
> modifications made to them inside the function change the variables
> values outside the function, since they are global. Had they been UDTs
> passing by reference or const reference may have been preferable. Had
> they belonged to another object or been declared within function body,
> passing by reference may have been necessary.

I understand.

> I see no reason at all that this entire namespace can't be neatly made
> into a class. Why are you making everything global exactly?

According to my performance test, global variables and global functions
are running faster than local variable and local function inside class. You
don't need to use a pointer to access global variable or global function.
You create a class. You define local variable and local function inside
class. The class requires a pointer to access local variable or local
function inside class. It may be slow because extra x86 instruction has to
be added to read memory address and then locate value in the memory.

> Also, are you sure you know what you are doing with the "static"
> keyword here?

Yes, I understand "static" keyword. It has two different definitions.
If you have same variable name on the global scope and local scope, you put
static inside function. Local variable inside function can modify global
variable outside of function. This way, you exit function, you won't lose
local variable to be placed in global variable. It is fine. I don't use
this feature.
Another definition for static is internal linkage. All global variables
and global functions have static keyword so they are inside internal
linkage. The global variable and global function using internal linkage on
the one header code is never available to all source codes unless you want
to use public function (you put extern keyword for public) in turn to access
hidden function. It is good design.
Please tell me what you think how to design object better -- use global
object for performance reason or local object for readability and reduce
bugs.

Yours Truly,
Bryan Parkoff