Thread: passing a class member function by reference?

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    33

    passing a class member function by reference?

    Is it okay to do this?

    Code:
    #include<iostream>
    
    
    int main()
    {
    
        class someclass
        {
         public:
         void function(someclass&);
         
        // private:
         int a;
         };
         someclass someclassobject;
         someclassobject.a=16;
        
     cout<<someclassobject.a<<endl;
     function(someclassobject.a);
     cout<<someclassobject.a;
    
     return 0;    
    }
    
    void someclass::function(someclass& a)// error here
    {
     a=18;
    
    }

  2. #2
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953

    Re: passing a class member function by reference?

    Originally posted by m712
    Is it okay to do this?

    Code:
    #include<iostream>
    
    
    int main()
    {
    
        class someclass
        {
         public:
         void function(someclass&);
         
        // private:
         int a;
         };
         someclass someclassobject;
         someclassobject.a=16;
        
     cout<<someclassobject.a<<endl;
     function(someclassobject.a);
     cout<<someclassobject.a;
    
     return 0;    
    }
    
    void someclass::function(someclass& a)// error here
    {
     a=18;
    
    }
    function(someclassobject.a);
    Is wrong because the function takes a someclassobject, not an integer so you should call like this:
    function(someclassobject);

    void someclass::function(someclass& a)// error here
    {
    a=18;

    }
    a = 18 should be a.a=18, because in a = 18, a is the object, you'll have to overload the assignment operator to get a=18 to work.
    none...

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    Code:
    void someclass::function(someclass& a)// error here
    {
     a=18;
    
    }
    Here you're passing an object of someclass, called a to the function. You don't need to pass the int data member a to the function, as it is part of the class and already accessable

  4. #4
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Ah, no.. there are a couple of errors here.

    1. You using cout and endl which is defined in the "std" namespace. To use these you must do this:

    // do this
    #include <iostream>
    using namepace std;

    // or this
    #include <iostream>
    using std::cout;
    using std::endl;

    2. You have declared the class within main, however the definition of the member function "function" is outside the scope of this declaration. In otherwords, the compiler will not recognise this declaration. It should be more like this..

    Code:
    #include<iostream>
    
    using std::cout;
    using std::endl;
    
    class someclass
    {
    public:
        void function(someclass&);
         
    private:
        int a;
    };
    
    void someclass::function(someclass & a)
    {
    	// some code here
    }
    
    int main()
    {
    
    	someclass someclassobject;
        
    	return 0;    
    }
    3. "someclassobject.a=16;" This line of code is correct, while the member variable "a" is declared as public, but this is not good practise. It looks as though you intended for this to be private, which is more in keeping with OOP principles. You would then have a member function that sets the value of "a" to whatever you desire.

    i.e.

    Code:
    void someclass::setValue(int b)
    {
         a = b;
    }
    and then you can use another class to return the value of a

    Code:
    int someclass::outputValue(void)
    {
    	return a;
    }
    4. With regards to passing the class to the function and then adding an integer value to the class. This is not possible to do in this fashion. You will want to look up operator overloading and "operator+".

    However, if what you were trying to achieve was to initialise or set the value of a, check the previous bit of code I posted regarding setValue() member function.
    Last edited by foniks munkee; 12-08-2002 at 05:04 AM.
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    33
    Thanks for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Class member function not recognized??
    By pwnz32 in forum C++ Programming
    Replies: 4
    Last Post: 08-23-2008, 03:33 AM
  3. class reference function
    By elninio in forum C++ Programming
    Replies: 1
    Last Post: 07-14-2008, 11:59 AM
  4. Replies: 6
    Last Post: 04-27-2006, 10:55 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM