Thread: passing an object as an argument to a function (by reference)

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    28

    passing an object as an argument to a function (by reference)

    Hi,

    how can I pass an object as an argument to a function. My knowledge of C++ is NULL but I do cope well with C so please accept my apologies if my question is rubbish. Basically here is my problem. Suppose there is a class call FooClass with some kind of contructor for two integers. In order to create an object I guess I would do something like

    FooClass Foo(1,2);

    Now I want to pass my Foo object by reference into a function called bar.

    I would normarlly do something like
    void
    bar(FooClass *foo);

    but I guess the above is completely wrong.... my compiler in any case is complaining (g++), as I think there is no constructor..... the thing is taht I don't want to create a new object but tell the bar function to expect a FooClass object.

    Both google search and looking in the forum failed, mainly becayse I am not sure I am asking the question right....

    Cheers.

  2. #2
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    To pass a reference to an object so that you can change it, you can do this:
    Code:
    void bar ( FooClass& foo );
    To pass a reference to an object so that you can't change it, you can do this:
    Code:
    void bar ( const FooClass& foo );
    Can you post your exact code and the errors that you're getting if that doesn't work?

    Cheers!

  3. #3
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by Anubis
    I think you might want to take a look at the "friend" keyword.
    no, that has nothing to do with the ops problem. why would you need the friend keyword here?

    If the compiler is complaining about missing a constructor I'm inclined to believe it.

    Ulysses post the definition of FooClass, and where you're calling bar().
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    28
    Quote Originally Posted by Slacker
    To pass a reference to an object so that you can change it, you can do this:
    Code:
    void bar ( FooClass& foo );
    This works just fine. Cheers.
    Last edited by odysseus.lost; 12-15-2005 at 04:09 AM.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    I thought to pass by refrence you did something like this
    Code:
    #include <iostream>
    
    using namespace std;
    
    void test(int * get);
    void undo(int * get);
    void print(const int * get);
    
    int main()
    {
    	int pass = 5;
    
    	cout << pass << endl;
    
    	test(&pass);
    
    	cout << pass << endl;
    
    	undo(&pass);
    	print (&pass);
    	
    	return 0;
    }
    void print(const int * get)
    {
    	cout << *get << endl;
    }
    void undo( int * get)
    {
      *get = *get / 25;
    }
    
    void test(int *get)
    {
    	*get = *get * *get * *get;
    }
    instead of
    Code:
    void test(int &get);

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I thought to pass by refrence you did something like this
    In C++, you can pass by reference using a pointer (like you did) or a reference (like Slacker showed). Using a reference is more common in C++, although there are different situations when each is better than the other.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing function as argument
    By NeMewSys in forum C Programming
    Replies: 16
    Last Post: 06-11-2008, 02:03 PM
  2. Warning while passing array by reference to function
    By Jdo300 in forum C Programming
    Replies: 11
    Last Post: 06-10-2008, 01:40 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. How to: Use OpenGL with Jgrasp
    By Pickels in forum Game Programming
    Replies: 3
    Last Post: 08-30-2005, 10:37 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM