Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Pass by Reference Function Question

Reply
Thread Tools

Re: Pass by Reference Function Question

 
 
Christopher
Guest
Posts: n/a
 
      04-11-2008
On Apr 11, 3:24 pm, "Bryan Parkoff" <(E-Mail Removed)> 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

> 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?


> 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.


> 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.


 
Reply With Quote
 
 
 
 
Christopher
Guest
Posts: n/a
 
      04-11-2008
On Apr 11, 5:00 pm, "Bryan Parkoff" <(E-Mail Removed)> wrote:
> > On Apr 11, 3:24 pm, "Bryan Parkoff" <(E-Mail Removed)> 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



Oh my goodness where to begin...

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"

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 see no reason at all that this entire namespace can't be neatly made
into a class. Why are you making everything global exactly?

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



 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      04-11-2008
"Victor Bazarov" <(E-Mail Removed)> quotes:
>>namespace Object
>>{
>> static int a = 0; // hidden global variable


Both a named namespace and file scope rarely makes sense.

To declare an identifier with external binding and
file scope, in C++, an anonymous namesspace is used:

namespace { int a = 0; }

»static« was used in C for this purpose, but is
obsolecent in C++.

 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      04-11-2008
Supersedes: <(E-Mail Removed)-berlin.de>

"Victor Bazarov" <(E-Mail Removed)> quotes:
>>namespace Object
>>{
>> static int a = 0; // hidden global variable


Both a named namespace and file scope together
rarely makes sense, because a namespace only helps
to separate names when they have have external linkage,
i.e., are visible to many parties.

To declare an identifier with internal linkage and
file scope, in C++, an anonymous namesspace is used:

namespace { int a = 0; }

»static« was used in C for this purpose, but is
obsolecent in C++.

 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      04-11-2008
http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Stefan Ram) writes:
>obsolecent


»obsolescent«

 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      04-12-2008
"Bryan Parkoff" <(E-Mail Removed)> writes:
>>»static« was used in C for this purpose, but is
>>obsolecent in C++.

>Then, why do you claim **static** keyword to be obsolete for both global


In C++, »static« is only obsolescent if it is used to indicate
internal linkage, because in C++, an anonymous namespace is
intended to be used for this purpose.

Other uses of »static« are not deemed obsolescent.

 
Reply With Quote
 
Paul Brettschneider
Guest
Posts: n/a
 
      04-13-2008
Bryan Parkoff wrote:

>>>> static int a = 0; // hidden global variable

>>
>> Both a named namespace and file scope rarely makes sense.
>>
>> To declare an identifier with external binding and
>> file scope, in C++, an anonymous namesspace is used:
>>
>> namespace { int a = 0; }
>>
>> »static« was used in C for this purpose, but is
>> obsolecent in C++.

>
> If you write C source code, you always want most functions to be
> invisible inside one header and source code using internal linkage. It is
> best to guard these functions. It is ideal when you make to design a
> chip. You do not want people to do reverse engineering to see inside chip
> and draw schematic.
> Then, why do you claim **static** keyword to be obsolete for both
> global
> variables and global functions?


Because for this you use anonymous namespaces, as multiple people have
already pointed out.

> I ask you please provide me an example of
> your source code in C++. Please do not tell me if I have to use class. I
> do not want to use a pointer to access memory address and locate variable
> and function inside class. It is slower than global function without
> pointer according to my performance test.


You keep repeating that, but have you actually really tried it?

The following code:
class dummy {
public:
int a;
int b;
void set() { a = 5; b = 6; };
};
namespace {
int a;
int b;
dummy c;
};
void f1()
{
a = 5;
b = 6;
}
void f2()
{
c.set();
}

produces on IA32 with g++4.2 -O3 -Wall -fomit-frame-pointer:

_Z2f1v:
movl $5, _ZN37_GLOBAL__N_static.C_00000000_D9D9A4DE1aE
movl $6, _ZN37_GLOBAL__N_static.C_00000000_D9D9A4DE1bE
ret

_Z2f2v:
movl $5, _ZN37_GLOBAL__N_static.C_00000000_D9D9A4DE1cE
movl $6, _ZN37_GLOBAL__N_static.C_00000000_D9D9A4DE1cE+4
ret

That is exactly the same code for the "global variables" version and
the "class" version. Of course, if you don't use inline methods, there will
be use of a this pointer, but I have a hard time imagining an application
where this makes any significant difference.

And if I understand the question in your first post (which I doubt): then it
will be better to only pass *one* "this" pointer than references (which are
probably implemented as pointers) to *multiple* variables. Just use
classes.
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      04-14-2008
"Victor Bazarov" <(E-Mail Removed)> writes:
>>In C++, »static« is only obsolescent if it is used to indicate
>>internal linkage, because in C++, an anonymous namespace is
>>intended to be used for this purpose.

>That is simply not true. Items declared in an anonymous namespace


Sorry for my false assertion and thanks for the correction!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
difference between pass by address and pass by reference!! blufox C Programming 2 04-03-2006 02:53 PM
pass by reference vs pass by pointer LuB C++ 6 09-23-2005 06:19 AM
Pass by reference / pass by value Jerry Java 20 09-09-2005 06:08 PM
Pass by Pointer * or Pass by Reference &? Robert C++ 10 08-24-2005 04:15 PM
Pass-by-reference instead of pass-by-pointer = a bad idea? Mr A C++ 111 07-14-2005 03:04 AM



Advertisments