Passing objects

This is a discussion on Passing objects within the C++ Programming forums, part of the General Programming Boards category; I'm having trouble passing objects to other functions. What are the guidlines for passing objects? Thanks....

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    28

    Passing objects

    I'm having trouble passing objects to other functions. What are the guidlines for passing objects? Thanks.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    What does the function do to the object?

    Kuphryn

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Basically, just past the reference to the object as the function prototype below indicates
    Code:
    returnValue myFunction( MyObject &object );   // If you need to modify object
    returnValue myFunction( const MyObject &object );  // If you don't modify object
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    If you are trying to pass an object without the & symbol (by reference) and without the * (as a pointer) then you are trying to pass it as value. This requires a copy to be made, so the class must have a copy constructor. This is a bad practice though because it causes a duplication of the object, which is time consuming and eats the stack.

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Theres no need to pass it as const reference just pass the class by value.
    Code:
    class test
    {
    public:
           int yep;
    };
    void function(test hello);
    
    int main()
    {
      test brian;
      brian.yep = 10;
      function(brian);
      std::cout<<brian.yep;
      std::cin.get();
    }
    
    void function(test hello)
    {
      hello.yep = hello.yep + 10;
      std::cout<<hello.yep;
    }
    Last edited by prog-bman; 10-06-2004 at 09:50 AM.
    Woop?

  6. #6
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    >> Theres no need to pass it as const reference just pass the class by value.
    As mentioned by pablo, pass by value requires a copy to be made. An object tends to be large in size (larger than primitive types i.e. int). So it'd be better if you pass the reference and add 'const' so that the object's member data is retained in a function.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing local objects by reference
    By manav in forum C++ Programming
    Replies: 15
    Last Post: 03-31-2008, 07:32 AM
  2. Newb Question on Passing Objects as Parameters
    By Mariano L Gappa in forum C++ Programming
    Replies: 12
    Last Post: 11-29-2006, 12:08 PM
  3. passing objects
    By r0bbb in forum C++ Programming
    Replies: 3
    Last Post: 03-05-2005, 12:10 PM
  4. Passing Objects using menus
    By TankCDR in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2003, 10:11 AM
  5. Passing objects
    By Wiggin in forum C++ Programming
    Replies: 1
    Last Post: 10-17-2001, 08:00 AM

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