Re: Pass by Reference Function Question

Discussion in 'C++' started by Christopher, Apr 11, 2008.

  1. Christopher

    Christopher Guest

    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

    > 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.
    Christopher, Apr 11, 2008
    #1
    1. Advertising

  2. Christopher

    Christopher Guest

    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



    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?
    Christopher, Apr 11, 2008
    #2
    1. Advertising

  3. Christopher

    Stefan Ram Guest

    "Victor Bazarov" <> 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++.
    Stefan Ram, Apr 11, 2008
    #3
  4. Christopher

    Stefan Ram Guest

    Supersedes: <-berlin.de>

    "Victor Bazarov" <> 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++.
    Stefan Ram, Apr 11, 2008
    #4
  5. Christopher

    Stefan Ram Guest

    -berlin.de (Stefan Ram) writes:
    >obsolecent


    »obsolescent«
    Stefan Ram, Apr 11, 2008
    #5
  6. Christopher

    Stefan Ram Guest

    "Bryan Parkoff" <> 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.
    Stefan Ram, Apr 12, 2008
    #6
  7. 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.
    Paul Brettschneider, Apr 13, 2008
    #7
  8. Christopher

    Stefan Ram Guest

    "Victor Bazarov" <> 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!
    Stefan Ram, Apr 14, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerry
    Replies:
    20
    Views:
    7,927
    Roedy Green
    Sep 9, 2005
  2. blufox
    Replies:
    2
    Views:
    551
  3. Mr A
    Replies:
    111
    Views:
    2,107
  4. Robert
    Replies:
    10
    Views:
    1,367
    E. Robert Tisdale
    Aug 24, 2005
  5. LuB
    Replies:
    6
    Views:
    452
    Mike Wahler
    Sep 23, 2005
Loading...

Share This Page