pass by referrence

This is a discussion on pass by referrence within the C++ Programming forums, part of the General Programming Boards category; For clarity, when I pass by referrence like this: Code: class Something { public: int i; char c[20]; long l; ...

  1. #1
    Unregstered
    Guest

    pass by referrence

    For clarity, when I pass by referrence like this:

    Code:
    class Something
    {
    public:
       int i;
       char c[20];
       long l;
       float f;   
    };
    
    void DoSomething(Something &something);
    the overhead is the same as if I passed a pointer like this:

    Code:
    void DoSomething(Something *something);

    as opposed to if I pass by value like this:

    Code:
    void DoSomething(Something something);
    in which case the overhead is sizeof(Something).
    Is that all correct?

  2. #2
    looking for the truth moemen ahmed's Avatar
    Join Date
    Feb 2002
    Location
    Egypt
    Posts
    161
    quote:
    --------------------------------------------------------------------------------
    C++ programmers strongly prefer references to pointers. References are cleaner and easier to use, and they do a better job of hiding information.
    --------------------------------------------------------------------------------


    quote:
    --------------------------------------------------------------------------------
    References cannot be reassigned, however. If you need to point first to one object and then another, you must use a pointer. References cannot be null, so if there is any chance that the object in question may be null, you must not use a reference. You must use a pointer
    --------------------------------------------------------------------------------

    this was borrowed from "teach yourself c++ in 21 days"

    while passing by value is another story, as it copy the value to a new variable of the same type and let the function deal with the copy not with the original variable, so it doesnt affect the original variable you pass to it

    i hope its clear for u
    Programming is a high logical enjoyable art for both programer and user !!

  3. #3
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    passing by value is sometimes bad for an struct, or class

    1) if the object is big...OUCH! (inheritance..etc..)

    2) it can all the default constructor forever..i mean forever!

    that is all..
    nextus, the samurai warrior

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    yes, passing by reference and passing by pointer have exactly the same overhead. they will do the same thing internally by the compiler.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Speed test result
    By audinue in forum C Programming
    Replies: 4
    Last Post: 07-07-2008, 06:18 AM
  2. Replies: 3
    Last Post: 11-22-2007, 12:58 AM
  3. pass by referrence
    By lipi in forum C Programming
    Replies: 3
    Last Post: 02-24-2003, 04:09 PM
  4. pass be reference versus pass by value
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2002, 02:03 PM
  5. Replies: 3
    Last Post: 04-02-2002, 01:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21